Updating Score C#

Hi everybody, i have a problem, i must update the score. I have 2 scripts :the first detecs collision and the other updates the score. If Gameobject À collides with gameobject B(i duplicated B in 4 instances) int score+=10.I tried to use Sendmessage but it didn’t Work. How i can do that??
Thanks in advance :slight_smile: i use C#

You can use the static modifier.

Instead of doing public void IncreaseScore(), you can do public static void IncreaseScore instead. You’d then be able to update the score from any class, without having to instantiate a class.

Ex.

public class ScoreTracker
{
private static int Score = 0;

public static void IncreaseScore(int Amount)
{
Score += Amount;
}
}

If you want to be able to update it from another class, all you’d need to do is put

ScoreTracker.IncreaseScore(10);

if you detect the collision.

On the collision script get access to the component of the score script which holds the score variable and add another variable (ie public int scorePenalty = 10; ) then subtract the scorePenalty from the score variable.

Look into accessing components of other scripts.