Hello everyone, I am working on a small 2d endless runner and I got a small problem. I have the following function
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.name == "Player")
{
collectedCoins++;
gameObject.SetActive(false);
coinSound.Play();
Debug.Log(collectedCoins);
}
}
My character runs on the platforms collects coins the script above deactivates the coins after the character is touching them but the collectedCoins value is not increasing as it should. Each platform haves 3 coins one after another, when the character touches the first coin debug.log shows 1 as I collected the first coin, once i touch the 2nd coin and 3rd coin the debug.log shows 1 again without increasing the total number. Now if on the next platform we have another set of 3 coins when i collect them debug.log will show 2,2,2 if i have a 3rd platform again with 3 coins and I collect them it goes to 3,3,3 but after this if the following 1-2 platform have no coins and later I get another platform with coins and i collect them my total number of coins from above (3,3,3) goes down to 2,2,2 or even 1,1,1 for no reason. What can i do? Is there a trick with the OnTriggerEnter2D function?
Thanks in advance