Coin GUI Score

I’m making a first person Mario game, and I’ve stumbled across another problem. My coin that I have adds onto my score just fine and all, but when I duplicate the coin and put two coins, I can’t get score of 2 points. I get the point for the first coin but when I pick up the second coin it stays at one. Here’s my script

#pragma strict
public var coinSpeed = 5.0;
private var score = 0;
var guiScore : GUIText;
function Start()
{
	guiScore.text = "0";
}
function Update()
{
	transform.Rotate(Vector3.up * coinSpeed * Time.deltaTime, Space.World);
}
function OnTriggerEnter (other : Collider)
{
		Destroy(gameObject);
		score += 1;
		guiScore.text = "" + score;
}

That’s because you destroyed the game object that the script is attached to. Therefore, the score never gets updated because the score keeps getting deleted and replaced.

You should have something like a manager script that your script can inform to update the score.

I’m not a UnityScript guy, but why this is happening is because each coin has their own copy of score. That means when you pick up the first coin, it’s score changes from 0 to 1. However, every single other coin in the game still has THEIR “score” as 0.

There are a million ways to handle this problem. Generally, I would have something like a ScoreManager that listened to coins getting picked up and updated the score for the whole game. Then if there are any questions as to what the score is, the ScoreManager gets final say.

Thanks for the help guys, I figured it out. I made the score static… and it seemed to work!