- Home /
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.
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
@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?
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.
@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?
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
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.
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?
???
??
?
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
Follow this Question
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