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 CrucibleLab · Mar 26, 2015 at 09:43 PM · c#update function

Multiple UPDATE functions running simultaneously?

I got two GameObjects, a "player" object and an "enemy" object. I have a script assigned to each object and both scripts contain the UPDATE function.

I noticed an unexpected behavior for the first time while I was troubleshooting my code: It seems that both UPDATE functions are running simultaneously, and I could tell this simply by looking at the logs from both scripts. And this appears to be the case even when the running game is only focused on either one of the two objects.

Is this the default behavior? What if I had a dozen different objects with an UPDATE function for each? I would like to think that I'm missing something here (e.g., there is a way to ensure only one UPDATE is running at any given time), but my search on both Google and Unity user forums came up short to address this particular issue.

Any insight will be much appreciated.

Comment
Add comment · Show 6
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 hexagonius · Mar 26, 2015 at 09:54 PM 0
Share

Do you have a concrete problem? The normal behaviour is that all Update functions get called in a random order, but since it's one threat after another. Don't know about Unity 5. If you need a specific order you can set it in script execution order in project settings

avatar image CrucibleLab · Mar 26, 2015 at 10:00 PM 0
Share

@hexagonius - Not sure what you mean by "concrete problem". But, according to your response, it does sound like ALL update functions get called ALL the time (although they maybe in a certain order), right? Wouldn't that be a big resource drainer?

avatar image hexagonius · Mar 26, 2015 at 10:04 PM 0
Share

Well this is how every engine works. If you want objects to do stuff every frame, you must. Remove Update if you don't need it on objects that don't need that behaviour.

avatar image CrucibleLab · Mar 26, 2015 at 10:10 PM 0
Share

@hexagonius - So, just to clarify, all Update functions that are attached to other inactive objects do run all the time and there is no way (at least provided by Unity) to force a certain Update function to "sleep" or become inactive, correct?

avatar image hexagonius · Mar 26, 2015 at 10:12 PM 0
Share

Now you talk about inactive objects. If you mean GameObjects being inactive, their Updates won't be called. Same for only disabling just the script

Show more comments

2 Replies

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

Answer by lordlycastle · Mar 26, 2015 at 10:44 PM

Yes, all the update functions get called every frame, that is the expected/desired behaviour, because if you want to do any movement calculations, or any continuous calculations, you do it there. You can also do it FixedUpdate (which is called every 0.0334 seconds by default), and LateUpdate which is called after every frame is processed. You can have hundreds of game objects with separate empty update functions and it probably won't slow down the game, what matters is what you are doing in the update function.

No, you cannot just run one update function, to do that either remove it from the script or disable the script not the game object; though I can't think why you would want to do that. Have a look at Script Execution Order to see what else is called when you run a script.

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
-1

Answer by xdarned · Feb 10 at 03:25 AM

I'll ask this: You have two game objects, each one with a component, A and B. In A, I have an update with a Debug.Log("Script A"); In B, I have an update with a Debug.Log("Script B");

I run the game.

Which debug (in the console) will be displayed first?


Now the same question but this time I have only ONE game object with two components, A and B. What will be displayed in the console?


Point is: The movement script calculates a direction. The animations script uses that direction to calculate an angle and pass it to a certaing blend tree. (Nevermind why or how). So my question is: Is the direction variable of the movement script ready when the animations script need is?


???

??

?

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 Bunny83 · Feb 10 at 10:25 AM 0
Share

This is not an answer to the question that was asked here. If you have a question, why don't you use the "Ask a Question" button instead of writing an Answer to somebody else's question that is 7 years old and already answered?


If two scripts run at the default execution time, their execution order is not deter$$anonymous$$ed. For more insight see the Script Execution Order page that is linked in the actual answer here. There's a section about Update order. To quote:

In general, you should not rely on the order in which the same event function is invoked for different GameObjects — except when the order is explicitly documented or settable. (If you need a more fine-grained control of the player loop, you can use the PlayerLoop API.)

You cannot specify the order in which an event function is called for different instances of the same MonoBehaviour subclass. For example, the Update function of one MonoBehaviour might be called before or after the Update function for the same MonoBehaviour on another GameObject — including its own parent or child GameObjects.

You can specify that the event functions of one MonoBehaviour subclass should be invoked before those of a different subclass (using the Script Execution Order panel of the Project Settings window). For example, if you had two scripts, EngineBehaviour and SteeringBehaviour, you could set the Script Execution Order such that EngineBehaviours always updates before SteeringBehaviours.

You can change the script execution order in the settings but only to differentiate two different kinds of scripts. Having the same script attached multiple times (no matter if its attached to the same or seperate gameobjects), you can't rely on the update order. It's usually deter$$anonymous$$ed by the creation order of the objects which you often do not control. Since this is not documented, you can't and should not rely on it.

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

24 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Object creation in Update 1 Answer

Update breaks function 2 Answers

update() keeps getting called with variable's old (initial) value, why wont the variable keep updated? 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