Taking variables from same script but different objects.

So I am making a 3d fighting game where at it’s current state has 1 Script for all the fighters. Tho i am trying to make it so i can simply change their stats so they are different(like attack and defense think pokemon) But i need to take the opponent’s attack and defense stats and put it in an equation to calculate the damage dealt. All the stats are in a public int format so i can change them on the unity editor. If it isn’t clear i need to take a variable from the same code on a different object. Hope this made sense.

So you have a few options but the two simplest are variations of the same thing.


Either you create a method and you pass in a reference to the instance player you want to attack i.e.

public double Attack(Player player, double amount) 
{
    player.health -= amount;
    return player.health;
}

Which would be called like:

somePlayer.Attack(someOtherPlayer, 100);

Or alternatively you could have a method on the player which you call from outside the player i.e.

// In The Player Class

public double Attack(double amount) 
{
    this.health -= amount;
    return this.health;
}

Which you can then call anywhere else like:

somePlayer.Attack(100);

Does that help? In both situations you have a reference to both objects so you can do calculations based on their internal members or using other internal methods. So it’s really down to how you want to structure your code.