- Home /
I have a problem with coins
//I'm using this inside my gamemanager script
points.text = "Points:" + player.currentPoints.ToString();
// I'm using these inside my player script
internal int currentPoints;
internal int point = 10;
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Coin")
{
currentPoints += point;
Destroy(other.gameObject);
}
}
------------------
I have a coin which was supposed to give me 10 points, but sometimes it gives me 10, sometimes 20 or 30. Can anyone explain what's wrong with my code? (sorry for my english by the way)
Answer by upasnavig90 · Feb 12, 2018 at 12:01 PM
This is happening because OnTriggerEnter is called multiple times, as there are number of collisons happen at that movement. You should use some other method for adding points or take a bool( if first collision happens it should not go for another one)
Just make sure you're using one collider on your player. That's why lots of player characters have hit BOXES. Basically one box collider that's the height and width of the player.