- Home /
OnTriggerEnter2d gets called twice sometimes.
Hi, I have a 2d game where every time my character hits a trigger collider I add a point. Most of the time it adds one point like its supposed to, but sometimes (about one in every ten or so) it will add two points. I only have one object with script on it, and have looked at other people's solutions but I can't figure it out. It seems to just be a unity problem to me. I have also tried debug logging and it logs twice whenever it adds two points. Any help would be much appreciated. Thanks. :)
So, your character is moving through and if it hits an object with a collider, your character would call the Trigger function ?
I'd like to see your code please @jobo22
Hi the object I hit has the Trigger function. Here is the code.
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
scores = true;
}
}
private void Update()
{
if (scores == true)
{
Score.AddPoint();
scores = false;
}
}
This is the Score.AddPoint() code
public static void AddPoint()
{
if (instance.bird.dead)
{
return;
}
score++;
if (score > highscore)
{
highscore = score;
PlayerPrefs.SetInt("highscore", highscore);
}
}
Answer by jobo22 · Feb 28, 2018 at 07:10 PM
Hi everyone I fixed the problem by switching the collider on my player from a 2D polygon collider to a 2D circle collider. Interesting that the 2D polygon collider was having issues as it wasn't very complex. But the 2D circle collider is working perfectly so I'm happy.
Answer by Xpartano · Feb 28, 2018 at 02:26 PM
Well having seen your script, I have something to clarify. First of all, why would you use the OnTriggerEnter2D event to set a variable to true and then on Update check if that variable is true to add score to your scoreboard? I suggest to do the following changes:
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
Score.AddPoint();
}
}
private void Update()
{
//Use this normally, use the Score.AddPoint() event in the OnTriggerEnter2D event
}
That should solve your problem. I recommend you to read the Documentation related to OnTriggerEnter2D. You can find it here: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html
I hope I've helped you! If not, let me know it!
Hi the reason I had the AddPoint in the update was because I was having this problem before with it in the OnTriggerEnter2D so I tried doing it this way. I have solved the problem though. I had a 2D polygon collider on my player and I switched it to a 2D circle collider and now it works perfectly. I don't know why the polygon collider wouldn't work. It wasn't very complex. But I have my solution and am satisfied with it. Thanks for taking the time to help! :)
Answer by Skyunarankage · Feb 28, 2018 at 02:34 PM
do you have 2 objects with collision ? maybe one on the player
Hi, I fixed my problem by switching from a 2D polygon collider to a 2D circle collider on my player. Not sure why polygon collider wasn't working, but it's not a big deal to me. Thanks for the help! :)
Your answer
Follow this Question
Related Questions
Unexpected token: collider? 1 Answer
OnTriggerEnter2D is being called multiple times.... 1 Answer
OnTriggerEnter2D & OnCollisionEnter2D Not Responding 1 Answer
score counter is not accurate 1 Answer
removing duplicates in list 2 Answers