Coin value is doubling on some scenes and not others?
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.
Add some Debug.Log statements in various parts of your code so you can see how things are flowing. Often this sort of doubling thing is because you have your script on multiple objects or something. Logging will help you identify if that is the case.
hey, I added a debug.log for the CoinPickup script.
On the scenes where it doubles the value the debug instantly appears twice. so it seems to be registering that the coin is picked up twice. I have searched everywhere and I am confident the script has not been placed twice into the scenes that it occurs with.
do you have any other advise? the coin is a prefab i created in blender, could that have any effect on it?
Thanks @ Dave Carlile
Ok, if I take the scene that works properly and then copy and paste all the assets from the scene that doesn't work then it still doubles meaning it must be tied to an in game object??? but I really cant find it, any suggestions?
Answer by Shire_ · Apr 22, 2016 at 10:03 AM
Fixed Script by replacing scripts attached to player in scenes where it was doubling.....