Search Unity

Script Execution Order Disregarded for New Objects

Discussion in 'Editor & General Support' started by ArtyBoomshaka, Sep 30, 2019.

  1. ArtyBoomshaka

    ArtyBoomshaka

    Joined:
    Mar 5, 2013
    Posts:
    226
    Hi,

    I've been struggling with working around what seems to be a pitfall of the Script Execution Order settings.

    While it seems to work for objects placed in the scene at load, it seems to be completely disregarded for the same objects instanced through scripts.

    Let's say I have a game object with components A and B (that needs A's Start() executed before its own).

    When created in the scene editor, the game object is valid, A.Start() is executed before B.Start() and all references are valid.
    When created by another script that way :

    Code (CSharp):
    1. var go = new GameObject();
    2. A a = go.AddComponent<A>();
    3. // setting up a
    4. go.AddComponent<B>();
    The B events are called first, for some reason.
    I can't init the A component in its Awake method because I need the setup comment from the snippet to be done first.

    I'm guessing this is all because the components are added/created one after the other to an already instanced GameObject, thus somehow breaking the script execution order but doing a
    Code (CSharp):
    1.  GameObject.Instanciate(go);
    after the setup didn't change anything in the execution order either so I'm at a loss.

    Edit : Tried instancing a prefab with both the components already on it but again, didn't work. :(

    Is this normal? I feel like I'm missing something, I can't be the only person to go through this and I've seen no mention of this case anywhere in my research before posting.


    Cheers,
     
    Last edited: Sep 30, 2019