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 Roixo · Feb 07, 2018 at 08:01 PM · ontriggerstay

OnTriggerStay execution script time can last more than 1 frame?

I have a simple but an important question.

Imagine that i have this script attached to any gameobject where the OnTriggerStay expend much time executing the code:

 void OnTriggerStay (Collider other)
 {
     for (int i = 0; i < 100000; i++) 
     {
         GameObject gameObj = GameObject.FindGameObjectWithTag ("anyTag");    
     }
 }

Is it possible that the execution time of script inside the OnTriggerStay can last more than the time between one frame and the nextone? How can I check it?

Comment
Add comment · Show 4
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 imM4TT · Feb 07, 2018 at 08:05 PM 0
Share

simply add print(i);

avatar image TreyH · Feb 07, 2018 at 09:26 PM 0
Share

hex has already said that it can't, but are you asking because you're curious or because you want it to?

avatar image Roixo TreyH · Feb 07, 2018 at 11:10 PM 0
Share

Because i want to know if it something that i need to keep in $$anonymous$$d.

avatar image hexagonius TreyH · Feb 09, 2018 at 08:00 AM 0
Share

I said it can't

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by hexagonius · Feb 07, 2018 at 09:23 PM

Simply, it can't. you could just log within update and fixedupdate and see that they're alternating. it's possible that there's multiple fixedupdates between frame updates or none at all, but you won't get a long executing OnTriggerStay to be bypassed by update. it's the same thread.

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 Nimosh · Dec 28, 2021 at 01:58 AM

You can use courutines to do task that takes more that one frame.

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 andrew-lukasik · Dec 27, 2021 at 10:34 PM 0
Share

Your answer should be more specific that this. void OnTriggerStay () => StartCoroutine("MyCoroutine"); is hardly a practical solution.

Very specific but somewhat reductio-ad-absurdum-question isn't helpful, I agree.

avatar image
0

Answer by endasil_unity · Dec 28, 2021 at 11:45 AM

You will stay on the same frame until the code in OnTriggerStay is done. Something that is important to understand is that the code execution is linear in Unity by default. If you have some code that takes a long time nothing else will be done until that code is complete. If your code in OnTriggerStay would take 30 seconds to complete, your game will freeze for 30 seconds.

If you want to perform tasks that take a lot of time and perform them over multiple frames, coroutines are a good thing to look at. You would perform a part of the task and then do a yield return to continue on the next frame.

Here you can read up on coroutines if you're curious. https://docs.unity3d.com/Manual/Coroutines.html

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

78 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

Related Questions

How to stop an Object from Moving When It Hits Collider 2 Answers

OnTriggerStay2D working only when player ISN'T moving 2 Answers

Ontrigger if 2 Answers

Layermask for OnTriggerStay 2 Answers

Alternative to OnTriggerStay2D 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