Search Unity

Execution order of Awake, Start AND custom init functions

Discussion in 'Scripting' started by alph, Dec 2, 2010.

Thread Status:
Not open for further replies.
  1. alph

    alph

    Joined:
    Jul 20, 2010
    Posts:
    89
    So I understand that all Awake functions are called before Start functions, but what about custom init functions?

    Here is my scenario:

    - I Instantiate a gameobject that have two component scripts. Lets just call them Script1 Script2. Script1 has my init function which I'll just call InitFunc.

    - Since one cannot pass variables through a constructor, I call InitFunc immediately after Instantiation.

    - Then I notice that things done in InitFunc will not be picked up in the Awake function of Script2, but it will be picked up by the Start function of Script2.

    So, since I couldn't find anything about this in the docs, I have to ask: Can I be sure of this? ..or was I just lucky that the init function of Script1 was executed before the Start function of Script2?
     
  2. melmonkey

    melmonkey

    Joined:
    Mar 31, 2009
    Posts:
    373
  3. alph

    alph

    Joined:
    Jul 20, 2010
    Posts:
    89
    Thanks, but I'm not that much wiser. I couldn't find my scenario into any of these docs.

    Code (csharp):
    1.  
    2. GameObject tmp = Instantiate(obj, pos, rot);
    3. tmp.GetComponent<Script1>().InitFunc();
    4.  
    When exactly is InitFunc called compared to any Awake Start functions of tmp's component scripts?
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    add debug log at the start of each and test it.

    I'm pretty sure awake is the first, but as start happens a frame later, initfunc should be 2nd, with start 3rd
     
  5. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    Awake is called during the instantiate, so there is no way for you to pass it any data. Start is called just before a script's first Update, which certainly won't happen between those two lines of code, so it is guaranteed to happen after your init function.
     
    Bunny83 and OhWellStudios like this.
  6. alph

    alph

    Joined:
    Jul 20, 2010
    Posts:
    89
    Thanks tomvds, that's what I wanted to know. May I ask how you can say this with guarantee? I mean, could I have found this in the docs somewhere, or do you need 5 years of unity experience to know this? :)
     
  7. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
  8. alph

    alph

    Joined:
    Jul 20, 2010
    Posts:
    89
    Thanks andeeee, I read that page already as it was suggested in the first reply, but I will argue that it's far from clear where my InitFunc fits into this documentation. It's logical what tomvds says, that calling InitFunc right after the Instantiate should maybe happen before Start, but that situation is not handled in the document above, at least I can't seem to see it. So, I will take his word for it, but it would be reassuring to see it on some documented page. If you see something that I don't, please specify :)
     
  9. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    The reference page for Start mentions that it is called right before the first update.
     
  10. alph

    alph

    Joined:
    Jul 20, 2010
    Posts:
    89
    Yes I know, in my case however I'm calling a custom function (InitFunc) just after an instantiation. This function call is the one I can't seem to fit into this documentation.
     
  11. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    As Instantiate and the first update never happen in the same frame, you actually can.

    Frame 0: Instantiate
    -> Awake
    -> Whatever you call on it

    Frame 1: Update
    -> Start -> Update
     
  12. Grave188

    Grave188

    Joined:
    Dec 13, 2017
    Posts:
    10
    Order of execution:
    Awake
    OnEnable
    InitFunc
    Start
    Update
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Please note that you're replying to a 12 year old thread. This information is already available in several places including the docs here.
     
  14. Grave188

    Grave188

    Joined:
    Dec 13, 2017
    Posts:
    10
    In case someone comes to the thread and needs an answer, like me, it's fine. The link you provided doesn't say when custom Init() functions are called, and that's exactly what the question was about.
     
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    What do you mean by "custom Init()" function? If you mean when your own function is called, how could Unity document that? It's called when you call it. If it's called in one of the documented functions I have a link to then that documentation is perfectly valid.

    What you wrote just describes you calling that function presumably in "OnEnable". Sorry if I'm missing something here.
     
  16. Grave188

    Grave188

    Joined:
    Dec 13, 2017
    Posts:
    10
    Question:
    "So I understand that all Awake functions are called before Start functions, but what about custom init functions?"
    Context:
    "I Instantiate a gameobject"
    "Since one cannot pass variables through a constructor, I call InitFunc immediately after Instantiation"

    My answer:
    Custom init function called after Instantiatiate() is called after Awake() and OnEnable() but before Start() and Update()

    Order of execution:
    Awake
    OnEnable
    InitFunc
    Start
    Update
     
  17. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Yes, I can read. That doesn't change what I said.

    If I call my "custom init function" in Awake then it'll be during awake. If I call it during OnEnable, it'll be during OnEnable. If I call it during Start, it'll be during Start.

    What you wrote isn't a fixed thing. It's also in reply to a 11 year old post which has already been answered correctly.

    I only mention the necro because it's part of our Community Code of Conduct so a thread that's already been answered and has served its purpose.

    But this is already making a necro even longer so I'll leave it right there.
     
    Kurt-Dekker likes this.
  18. Grave188

    Grave188

    Joined:
    Dec 13, 2017
    Posts:
    10
    Context of question:
    Code (CSharp):
    1. GameObject tmp = Instantiate(obj, pos, rot);
    2. tmp.GetComponent<Script1>().InitFunc();
    Custom function called just after Instantiate().
    Custom function called not in Awake, Start, Update, Reset, OnValidate, not in coroutine and so on.

    Post is necro but my answer may be helpfull now.
    My answer is more specific and clear than what has been left before.
     
  19. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Yes, to a specific question to a user who hasn't posted in over 10 years. This doesn't help anyone else. You posted it, it's done but please try not to necro such old posts in the future.

    Thanks.
     
Thread Status:
Not open for further replies.