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.
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.
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?