- Home /
2D Coins and Points
I have 5 coins set up with Box Colliders 2D.
Here is the script that destroys the coins and adds points (AddPoints(1) just adds one point to a variable)
void OnTriggerEnter2D(Collider2D col) {
if (col.CompareTag ("coin")) {
gm.AddPoints (1);
Destroy (col.gameObject);
}
}
However, when I play my game, I get a total of 6-7 points. Is there a problem with my box colliders or do I need to use a different method to do this?
Cheers!
Are you sure you haven't attached the script 6-7 times on the same gameObject? I suppose gm
is your Game$$anonymous$$anager. How does it handle the score? How do you retrieve a reference to gm
?
The script is attached only once, and I get 6-7 points after having collected all 5 coins. I've tried with only one coin and I got only one point, which is good, but as soon as I have multiple coins, the points I get after collecting all the coins isn't what it should be.
And Game$$anonymous$$anager just adds one point to an integer everytime the AddPoints (1) is called. I reference it like this:
gm = GameObject.FindGameObjectWithTag ("Game$$anonymous$$aster").GetComponent ();
Answer by avra800 · Dec 22, 2018 at 08:08 AM
If you have more than one object on your character you need to remove the colliders from the subsidiary objects but leave the collider on the main object. Hope it helps.