• 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 /
  • Help Room /
avatar image
0
Question by Smurfj3 · Nov 03, 2015 at 08:51 PM · c#ontriggerenterobjectsontriggerexit

OnTriggerEnter and OnTriggerExit behaving unexpected

 void Start()
     {      
         objects = 0; 
     }
 
     // Update is called once per frame
     void Update()
     {
         Debug.Log(objects);
     }
 
     void OnTriggerEnter(Collider col)
     {
         objects ++;
         Debug.Log(col.name);
     }
 
     void OnTriggerExit(Collider col)
     {
         objects --;
         Debug.Log(col.name);
     }

If I add this script to a prefab that I have got and I move it into other objects it should show me how many objects it is colliding with. It does that but for some reason it says there is always 0 too. For instance I make sure the object collides with 3 other objects, it will debug saying 3, but at the same time it also debugs 0. its like every frame it says 0 and 3.

So for instance when I add a simple boolean saying: if(Objects == 5){ thisBoolean = true }, it will never actually set it to true, even if I make it collide with 5 objects, it will set it to true and then false again in the same frame.

I have no clue how to get this to work properly. I am pretty certain I had the code working on an earlier Unity version, but now at 5.1 it doesn't seem to work.

Please help me get rid of my headache :)

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

1 Reply

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

Answer by warrenblyth · Nov 03, 2015 at 09:09 PM

@Smurfj3 you might need to use FixedUpdate instead of Update? As i understand it FixedUpdate() only fires when physics have been updated. Update() fires as fast as Unity can, so it may updating so fast that it gives you the number of objects collided with both before and after the physics (trigger) is calculated.

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 Smurfj3 · Nov 04, 2015 at 03:11 AM 0
Share

All I can say is I love you, this thing was giving me a headache because at an earlier version of unity I had the script working fine so I couldn't figure it out, but that fixed it. Awesome! Thank you so much

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

OnTrigger Problems Between Colliders Issue 1 Answer

C# - How can i solve this door problem in script? Can someone help me? 1 Answer

math.Lerp not correct? 1 Answer

Help to play music whenever the character enters a GameObject... 1 Answer

OnTriggerExit2D doesn't work when one of gameObject setActive false. 0 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