I have made a prefab with a coin value of 5. It is picked up as the player collides with the coin and a score displayed on the canvas.
However, on some scenes the coin value is doubled for some reason (instead of recording a score of , a score of 10 is recorded instead) which is not intended.
I have tried deleting the coins/ prefab but when ever a new coin is added to the same scene it always doubles the value???
CoinValue Script:
public class CoinValue : MonoBehaviour {
public int value;
}
Score Script:
public class ScoreManager : MonoBehaviour {
public static int Money;
Text ScoreCounter;
void Awake()
{
ScoreCounter = GetComponent<Text>();
Money = 0;
}
void Update()
{
ScoreCounter.text = "Score: " + Money;
}
}
Collision Script:
public class CoinPickup : MonoBehaviour {
void OnTriggerEnter(Collider col)
{
if (gameObject.CompareTag("Coins"))
{
Destroy(gameObject);
ScoreManager.Money += gameObject.GetComponent<CoinValue>().value;
}
}
}
All of these scripts are then added to the prefab coin under the tag Coins.
Any help on this would be great because I’ve tried a couple of different ways of counting the coins collected but the problem seems to persist.
Thanks for reading.