Scripts, functions and order of call

On
file:///Applications/Unity/Documentation/ScriptReference/MonoBehaviour.html
there’s a walkthrough of different functions. But what if you make 2 different scripts which both of them has functions of the same name. Which function is run first? Does it depend on which of the scripts was attached to the object first or how does the engine deside it?

The call order of functions can be found somewhere on the forum or wiki if you search for it. It goes something like:

All Awake()'s
All Start()'s
All Update()'s
All LateUpdate()'s
Rendering

The order of individual calls are non defined. It might apear in the editor to be script first added, but that is not garantueed to be the case when you build.

Same goes for all other equal named function that the engine executes.

If you need to get a specfic order of executing of say Start() you need to implement the order yourself by doing some clever waiting and reporting.

PS: Found the link anyways unifycommunity.com

Regards,
Marc

So although the functions are called in a certain order according to their names it’s not sure which function is called first if they have the same name if I understand it correctly (a bug in Unity at the moment?) ?

It is not a bug. Works like that in most game engines. Its simply non defined. Which in term means that you should not make code that rely on a specific ordering.

Think we might be talking past each other. Now im not entirly sure what functions you are refering to here:

Maybe this will help.

Imagine you have 3 game objects (A, B and C) all with the same script attached something like this:

function Start() {
   // Do Init
}

function Update() {
   // Do update
}

Now when the engine run the game loop, which is run each frame. It will call Start() in a non defined order on the 3 game objects. Once that is done it will the call Update() in a non defined order on the objects.

Now the order which is garantueed by the engine is that all Start functions will be called before any Update functions are called. Meaning that A.Start(), B.Start() and C.Start is called before it starts calling A.Update(), B.Update() and C.Update().

It is not garantueed that the engine will always call A.Start() followed by B.Start() then C.Start() it might be different each time.

Does that make any sense?

I just think ir’s me who didn’t explain it good enough - sorry. I just used to the fact that it’s possible to set everything in order. But if you make 2 scripts with functions in each with the same name (for instance “start”) then the engine doesn’t know which one to choose first. Hence a random choice.

But you can, of course, get around this “issue”. Just keep good control of your names of the funtions and keep the naming of certain funtions to a minimum.

If you need things to happen in a certain order and the Update/LateUpdate events are not enough, have one “Master” update function call the other update functions in the desired order.

Example:

You have object A, B and C. These three objects use the same script.

Instead of naming the update function “Update”, which would cause the update functions to get called in some undefined order by Unity, name the update function something like “DoUpdate”. Then in your “Master” update function, which is named “Update” and thus will get called by Unity each update-cycle, you call the “DoUpdate” functions in the desired order.

function Update() // this is your 'Master" update function
 {
 A.DoUpdate();
 B.DoUpdate();
 C.DoUpdate();
 }