coin collecting script not working properly

I wrote this for Coin collecting in a 2d game I’m working on. Problem is that script isn’t counting properly, it sometimes counts two coins instead of one. Scripts is attached to the player. Could you help me with that, I’m new in this, this is my second project in Unity ever. Thank you.

using UnityEngine;
using System.Collections;

public class Coin : MonoBehaviour
{
public TextMesh coinCount;
public AudioClip coinCollect;

int CoinAmount;

void Update()
{
coinCount.text = “” + CoinAmount;
}

void OnTriggerEnter(Collider collider)
{
if (collider.tag == “dinar”)
{
AudioSource.PlayClipAtPoint(coinCollect, transform.position);
CoinAmount += 1;
Destroy(collider.gameObject);
}
}
}

2403712–164094–Coin.cs (528 Bytes)

You should use :

OnTriggerEnter2D(Collider2D col){
...
}

For 2D games.

Check that you don’t have multiple Colliders on your player cus that can cause OnTriggerEnter being called multiple times.

1 Like

thanks, there were multiple colliders, it works fine now.