- Home /
OnCollision and OnTrigger being called twice
So I've looked around for different ways to fix this issue. I've tried setting up bools to make sure the increment only happens once, tried adding an OnCollisionExit entry in conjunction with the first option, and even tried rethinking out the logic. However, nothing I seem to do fixes this issue and I'm at a loss for what is causing it right now.
Here is my OnCollision Function:
void OnCollisionEnter(Collision col)
{
//Ally
if (col.gameObject.tag == "Ally")
{
//Increment Slaves Freed
gameManager.SlavesFreed += 1;
//Update Visual HUD Counter
SetCountText();
//Destroy GameObject
Destroy(col.collider);
Destroy(col.rigidbody);
Destroy(col.gameObject);
}
}
Here is the OnTrigger:
void OnTriggerEnter(Collider col)
{
//PowerUp
if (col.gameObject.tag == "Powerup")
{
magicUses += 1;
Destroy(col.gameObject);
SoundManager.instance.PlaySingle(powerupSound);
}
}
EDIT: For those stopping in and looking for a solution to this as well, take a look at ScaniX's comments. It will help lead you down the right path to finding an issue.
My problem solved and I'm not sure what was different, but it could help you with your issue
Answer by ScaniX · Aug 23, 2016 at 01:50 PM
As the trigger enter seems to be called on the player and not the powerup, you could add a consume() to your powerup. I guess the easiest way would be just to kill the tag on contact.
void OnTriggerEnter(Collider col)
{
//PowerUp
if (col.gameObject.tag == "Powerup")
{
col.gameObject.tag = null; // or "Eaten" or whatever
magicUses += 1;
Destroy(col.gameObject);
SoundManager.instance.PlaySingle(powerupSound);
}
}
Still seems to be doing the same thing. I've tried bools and what you had just mentioned. It doesn't seem to leave the collision, but ins$$anonymous$$d just re-enters the statements inside regardless of the bools or other limiters I've tried.
That really does not make sense.
You should start printing out the object ids to see if you accidentally instantiated more than one player and/or powerup at the same place.
Debug.Log("$$anonymous$$y instance: " + GetInstanceID() + ", col instance: " + col.gameObject.GetInstanceID());
Answer by timothy92 · Aug 13, 2016 at 03:00 PM
I dont know why this happens, but an easy fix it to make it so it can only be called once using a bool, e.g:
bool called = false;
void OnTriggerEnter() {
// if the function hasnt been called, we can run it
if (!called) {
// Set called to true, so that it cant be called again.
called == true;
}
}
and if you needed the function to run multiple times, not only one, you could set a timer to reset the called variable
Hope this helps someone :)
I set up a bool system to do just that and it seems to ignore the bool altogether. I've set break points to track the functions and it will flip the bool go through like normal but then reset the value and jump right back into it.
Answer by aditya · Aug 13, 2016 at 07:22 AM
Try ..
bool disableTrigger;
void OnTriggerEnter(Collider col)
{
if(!disableTrigger){
disableTrigger = true;
//PowerUp
if (col.gameObject.tag == "Powerup")
{
magicUses += 1;
Destroy(col.gameObject);
SoundManager.instance.PlaySingle(powerupSound);
}
}
}
this code is working all fine for me If there is only one trigger there and not two
Your answer
Follow this Question
Related Questions
My spikes don't damage the player 2 Answers
Distribute terrain in zones 3 Answers
Character Fails To Jump Sometimes 1 Answer
Collision triggers do not work on child gameobject 3 Answers