- Home /
Why does this increment by 2?
The object collides with the player, turns invisible, plays the particle, then increases the score based on the object's color.
However, it increases yellowcount, bluecount, etc., by 2 instead of by 1 (same if it's yellowcount += 1). Happens on the first frame of collision. Also happens if it's OnTriggerExit2D.
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
renderer.enabled = false;
particle.Play();
switch (flagColor)
{
case 0: //yellow
score.yellowcount++;
break;
case 1: //green
score.greencount++;
break;
case 2: //blue
score.bluecount++;
break;
case 3: //red
score.redcount++;
break;
}
}
}
Awesome, yep! It's incrementing by the amount of active flags of that color. Thanks!
Not really an answer, but more of a comment. I personally believe it's a better practice to see when the player collides with an object, not when an object collides with the player. It simplifies things during more advanced development. It also reduces the amount of instances (links) to the score script you would need, increasing performance.
Answer by thaiscorpion · Feb 16, 2014 at 06:56 PM
I can't see any issue with that script that would produce this. I had a similar issue and it was because I had the script added twice in the scene so it did the calculations two times.
well, I removed all other instances calling the script. Just the player and one flag with an empty game object holding the score. It's going up by 2. It's maddening.
Add a Debug.Log in each case and check if it is being executed twice. If so then you must have the script twice or something :D
Answer by POLYGAMe · Feb 18, 2014 at 09:53 AM
Check that your player doesn't have more than one collider (possible child objects?) with the Player tag. Each one will trigger the code.
Your answer
Follow this Question
Related Questions
Enable Specific Collider when Another is Triggered. 1 Answer
Best practice for checking which object has collided. 2 Answers
How to work with tags ? 1 Answer
Checking if a single object's trigger is entered out of a group of identical objects? 1 Answer
Is It Possible to Detect a Collision Within a Canvas? 2 Answers