Search Unity

When will Awake be called?

Discussion in 'Scripting' started by ZO5KmUG6R, Jan 26, 2015.

  1. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Code (CSharp):
    1.  public static LuaComponent CreateComponent (GameObject g, string componentName,bool enabled = true) {
    2.   LuaComponent lc = g.AddComponent<LuaComponent>();
    3.   lc.componentName = componentName;
    4.   lc.enabled = enabled;
    5.   return lc;
    6. }
    Will Awake be called when g.AddComponent is called? Because this is not suitable and I'd like for it to be called after the return.
     
  2. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
  3. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    That doesn't tell me if it will be called directly after AddComponent or the next frame though. I'm curious because lc.componentName NEEDS to be set first.
     
    trombonaut likes this.
  4. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    244
    Code (CSharp):
    1. void Awake(){
    2. Debug.Log("Awake called");
    3. }
    4.  
    5. ...
    6.  
    7. LuaComponent lc = g.AddComponent<LuaComponent>();
    8. Debug.Log("Component Created");
    9.  
    10. ???
    11.  
    12. Profit
     
  5. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Well why didn't I think of that haha
     
  6. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Well crap, Awake is called before I have a chance to initialize the component :(

    EDIT : Any way to stop this?
    EDIT : Duh, I'm dumb, I should just make my own awake function :p

    Might help if I initialize the filesystem first haha
     
    Last edited: Jan 26, 2015
  7. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    Hmm..can you explain a bit more why you would like to initialize the component before ?

    Also, you could just do a return; in the Awake if you don't want it to execute immediately and
    later just call another method to initialize or do your stuff.
     
  8. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Because i have to load the code chunk into the Lua subsystem, then it will do it's precompile work, THEN I can call my custom awake function which would call the

    function Awake()
    --Do stuff
    end

    In the Lua file. I can't have it calling before since there is no loaded code to call.
     
  9. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    aah ok glad you figured out :)
     
  10. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    You could change the script execution order, Or try using OnEnable ()
    http://docs.unity3d.com/Manual/ExecutionOrder.html
     
  11. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Well I have it all working like I want to already :)