'Solidity Error: member balance not found or not visible after argument-dependent lookup in contract

I'm getting this error in Solidity for the following code snippet:

member balance not found or not visible after argument-dependent lookup in contract

function pickWinner() public {
    uint index = random() % players.length;
    players[index].transfer(address(this).balance); // error here
}

What's the problem?



Solution 1:[1]

Is your players array payable? Your code works for me:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

contract PickWinner {
    address payable[] players;
    function pickWinner() public {
        uint256 winnerIndex = 80 % players.length; // I chose 80 as a "random" value
        players[winnerIndex].transfer(address(this).balance); 
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Calypso