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 MoosaSaadat · Jul 19, 2018 at 06:39 AM · trigger2d-physicsontriggerenter2d

OnTriggerEnter2D called twice sometimes when IsTrigger is ON

I have a game object with only a box collider 2d (IsTrigger is ON) attached to it. I use this to detect ( using OnTriggerEnter2D() ) when player falls on it so that the life count is reduced by 1. But most of the times, it is called twice and life count decrements by 2. After a lot of debugging I found out that this issue only happens when IsTrigger is ON. It doesn't happen when IsTrigger is OFF. Why is it happening? Is there a problem in my code or is it some sort of unity error?

     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.gameObject.tag == "Deadly")
         {
             lifeCount = lifeCount - 1;
             player.SetActive(false);
 
             if (lifeCount <= 0)
             {
                 DiedPanel.SetActive(true);
             }
             else
             {
                //Some code here to reposition player
                //It is made sure that the player is not positioned on the Deadly.
                 player.SetActive(true);
             }
         }
   }
Comment
Add comment · Show 3
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 LanslowDuLac · Jul 19, 2018 at 07:36 AM 0
Share

Hi, Is it the only "OnTriggerEnter2D" function you have in your project? I'm sure it's not a Unity issue. This function is probably called twice when on trigger enter. So you have this GameObject with Trigger Collider and your Player? Could you give your Player hierarchy or collider?

avatar image MoosaSaadat LanslowDuLac · Jul 19, 2018 at 07:51 AM 0
Share

No, I have one other "OnTriggerEnter2D" as well. But it only does one thing: gameObject.SetActive(false). So, it is not interfering in this function. I have attached the images which I think you are inquiring about. If you need more information, please do tell me.alt text

colliders.png (5.6 kB)
hierarchy.png (9.7 kB)
avatar image MoosaSaadat · Jul 19, 2018 at 08:25 AM 0
Share

ONE $$anonymous$$ORE THING TO $$anonymous$$ENTION HERE: It also double counts the collisions between player and collectables. So when I pick up the coin (with a Circle Collider 2D), it increases the coinCount by 2 (not always but usually). Setting IsTrigger to false solves the problem in this case as well.

2 Replies

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

Answer by MoosaSaadat · Jul 19, 2018 at 02:58 PM

So, here is the solution (finally): I had isTrigger ON on both objects that were colliding. So both of their OnTriggerEnter2D was activated. Although, I have had attached the script on only the player, their was a double decrement. In short: Do not set both objects' IsTrigger to TRUE

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
avatar image
0

Answer by LanslowDuLac · Jul 19, 2018 at 07:59 AM

I think it's because you're using "player.SetActive()". Your Player is active, so Trigger is called, next you're using player.SetActive(false), so GameObject is deactivated and not in collision with your Collider, and "if lifeCount > 0" you're using SetActive(true), so Player is activated and enters again in collision, OnTriggerEnter is called.

Comment
Add comment · Show 3 · 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 MoosaSaadat · Jul 19, 2018 at 08:03 AM 0
Share

No. Actually, I reposition the player before resetting SetActive to true.

avatar image LanslowDuLac MoosaSaadat · Jul 19, 2018 at 08:08 AM 0
Share

Did you try to remove "player.SetActive(false)" and putting it in "if lifeCount <= 0" ? It's what you want no? Here you're deactivating GameObject and activating it in else condition, it's strange in my opinion :)

avatar image MoosaSaadat LanslowDuLac · Jul 19, 2018 at 08:21 AM 0
Share

Yes, I tried your solution. Unfortunately, it didn't work. The purpose of reactivating the player in else condition is as follows: If lifeCount is less than or equal to 0, game ends. In else condition, I want to continue the game. So reactivating the player is just like respawning the player. I hope you now get my algorithm, otherwise, I am too bad at making them xD Anyways, I have just set IsTrigger to false to get things working. It is doing fine until now.

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

108 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

Related Questions

Ontriggerstay2d only runs 26 times in a row? 3 Answers

Is There A Way To Disable a Trigger But Still Use OnTriggerExit2D? (For COOP switch management) 2 Answers

How to GetComponent once for multiple comparisions 2 Answers

OnTriggerEnter2D not working 2 Answers

Is there a way to access Child colliders trigger from parent 2 Answers


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