Update() function location (best practice)

Looking at some examples, I notice the following:

  • Some examples have a single Update() function on a script file
  • Some examples have multiple Update() functions on multiple files

I’d like to know when you want to use #1 or #2 technique.

What’s the recommended best practice?

Thanks in advance for your help.

To clarify what you mean in #2 - Do you mean “multiple script files, each with their own single Update() function” ?

priceap: Yeah it has to be. It won’t work otherwise.

-pion: Unity doesn’t care. It is up to you. If you want two separate objects to have their own game updates, make a script for each with their own updates. If you don’t, then don’t. It honestly is up to you.

Yes, each script file has its own Update() function in scenario #2 .

Then as hippocoder replied. I just wanted to be sure before saying you can do it either way.

The beauty of being able to develop scripts for individual object behaviors is balanced by the need to keep track of it all.

Also from seeing examples, I learned the practice of creating an empty game object in my scene (it might be called “main”) and on that object you keep scripts that manage global functions such as score, timekeeping, shared information, etc… Then other scripts on game objects are more individualized to the behaviors of those specific objects, and might send or receive values on the main scripts.

There is probably an appropriate term for it, but the point is that while you may have many scripts on many objects, there can still be an organized way to think about centralized scripts that manage stuff common to all the others.

The best practice is often not using Update, but coroutines and InvokeRepeating.

–Eric