Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by vexe · Sep 25, 2013 at 07:29 AM · triggerboxcolliderontriggerstaygetkeydownontrigger

'if' check inside an OnTriggerStay is called numerous times although it shouldn't? (DRIVING ME INSANE)

I remember helping someone who had a similar problem, but I can't seem to help myself.

I don't know what the heck, but I just have a trigger box collider, when the player steps in it and presses 'e' - I toggle on/off an item box (Similar to Resident Evil 1,2,3,0)

 private void OnTriggerStay(Collider other)
 {
     if (other.tag == Tags.player)
     {
         if (Input.GetKeyDown(KeyCode.E))
         {
             StartCoroutine(ToggleBag());
             cnt++;
             Debug.Log("WTF??! " + cnt + " FROM " + this); // I get like, 18 for the first time I press 'e', 37 for the 2nd, etc
         }
     }
 }

 private IEnumerator ToggleBag() // If I don't do this, Unity barks: "destroying gameobjects immediately is not permitted" 
 {
     yield return new WaitForEndOfFrame();
     itemBox.rootPanel.gameObject.SetActive(!itemBox.rootPanel.gameObject.activeSelf);
 }

And YES, I could use some boolean hacks, like:

 void OnTriggerEnter(Collider other)
 {
    if (other.gameObject.tag == Tags.player)
       isInside = true;
 }
 
 void OnTriggerExit(Collider other)
 {
    if (other.gameObject.tag == Tags.player)
       isInside = false;
 }
 
 void Update()
 {
     if (isInside && Input.GetKeyDown(KeyCode.E))
       StartCoroutine(ToggleBag());
 {

I'm not looking for hacks, I'm looking for an explanation as to why the check body is being executed more than once because it shouldn't, as Input.GetKeyDown returns true only in the frame the key is pressed in, and then it becomes false, so... does this mean it's getting executed more than once IN ONE FRAME??

And yes, I've tried Input.GetKeyUp -- didn't make a difference.

Any idea?

Thanks a bunch.

Comment
Add comment · Show 2
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 hamstar · Sep 25, 2013 at 03:04 PM 2
Share

I don't have an answer but maybe someone could elaborate on the response in this thread: http://forum.unity3d.com/threads/200356-Need-help-with-OnTriggerStay()-and-Get$$anonymous$$eyDown()

avatar image vexe · Sep 25, 2013 at 03:47 PM 0
Share
  • So... FixedUpdate runs differently than Update, and OnTriggerStay is like FixedUpdate, but I don't get how differently they opperate... I think the best way to understand this is to see it in action, make some debug logs here and there and see what's going on...

1 Reply

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

Answer by KiraSensei · Sep 25, 2013 at 03:36 PM

GetKeyDown method should be used only in the Update() method. See the doc.

OnTriggerStay is also called at every frame, but I don't really know when, compared to the Update method. So the "hack" you proposed yourself is quite exactly what you should do :)

Comment
Add comment · Show 2 · 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 vexe · Sep 25, 2013 at 03:44 PM 1
Share

'only' in Update()? - what about OnTriggerEnter and Exit? I think it works there...

avatar image KiraSensei · Sep 25, 2013 at 04:00 PM 1
Share

I'm not a pro, I can't tell you why your code does not work as expected. This is what is written in the doc ...

"You need to call this function from the Update function, since the state gets reset each frame". The OnTriggerEnter, exit or stay methods are called, but maybe too early or too late, and the input state gets reset each frame, but when exactly ? That is the question ...

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

18 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

Related Questions

OnTriggerStay simple question 1 Answer

OnTriggerEnter only working once. 0 Answers

CharacterController with BoxCollider 1 Answer

OnTriggerExit / OnTriggerEnter incorrectly called when changing collider center 0 Answers

Trigger not detecting tag 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