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)