Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Paul2357 · Aug 12, 2016 at 07:41 PM · c#collisiontriggerfunction

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
      }
  }
Comment
Add comment · Show 15 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Paul2357 · Aug 23, 2016 at 01:53 PM 0
Share

@ScaniX

I'll give that a shot right now

avatar image Paul2357 Paul2357 · Aug 23, 2016 at 01:58 PM 0
Share

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.

avatar image ScaniX Paul2357 · Aug 23, 2016 at 02:04 PM 1
Share

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

Show more comments
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Paul2357 · Aug 23, 2016 at 01:24 PM 0
Share

@timothy92

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.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

218 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

OnTriggerEnter2D/OnCollisionEnter2D - delay 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges