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

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);
     }
 }

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 :slight_smile:

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