- Home /
How can I use method to return custom classes (Same Class Type),How to return classes in method
How can I use method to return custom classes. Pass Class in method then return opponent class (Same Class Type "Player") Sorry for my bad grammar.
public class GameControl : MonoBehaviour
{
Player player;
Player enemy;
private void Start()
{
Stealing(enemy);
}
public void Stealing(Player _thief)
{
if (_thief.StealSucceed)
{
_thief.coins += 2;
Opponent(_thief) -= 2;
}
}
public Player Opponent(Player _player)
{
if(_player == player)
{
return enemy;
}
if (_player == enemy)
{
return player;
}
}
}
Comment
Answer by CodesCove · Jul 24, 2020 at 08:53 PM
few things..
1) your Opponent(_thief)-= 2;
misses .coins ? (no pun intended :) )
2) your mehtod public Player Opponent(Player _player) does just that.. returns reference to a specific instance of Player class (player or enemy)
3) it is preferred to check equality of object references (not value types) with .ReferenceEquals. in your case
_player.ReferenceEquals(player)
and
_player.ReferenceEquals(enemy)