Why is Score not Adding Up?

Hello,

My score is supposed to be going up by 1 each time I have a collision with an object. However, it only works once. Example, I pick up collide with an object and my score goes to 1, if I collide with another object my score stays at 1.

Here’s my code:

function OnTriggerEnter (collision : Collider) {

	if (collision.name == "Player") {
	
		Debug.Log ("COLLISION " + collision.transform.name);
		myScore ++;
		scoreGuiDisplay.text = "MY SCORE HERE " + myScore;
		
		Destroy(gameObject);
		
	}
	

}

Any help is greatly appreciated.
Thanks in advance!

The variable myScore increasing here is held by the enemy (see the name check, it’s “player”). But you’re destroying it afterward, and myScore is lost.

You can either keep that script but declare myScore on a script on your player, and access it here (collision.transform.GetComponent().score++), or put that script on the player, and reverse it, check if the name is something like “enemy” or whatever.

It seems variable ‘myScore’ is declared in the script which is being attached to other objects (not player). So, after incrementing, you are destroying object (Line.9). Hence, script will also be destroyed where ‘myScore’ is declared.

Declare ‘myScore’ in a global script or in a script attached to player.