Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

!m_DidAwake assertions

Discussion in 'Scripting' started by NCarter, Apr 16, 2006.

  1. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    I'm getting these assertions while attempting to add a script component to an existing object using GameObject.AddComponent():

    Code (csharp):
    1. !m_DidAwake (Filename: MonoBehaviour.cpp Line: 180)
    2. !m_DidAwake (Filename: MonoBehaviour.cpp Line: 119)
    It's also telling me that a reference is null on a particular line, when it apparently isn't if I look at it in the inspector.

    I'm still debugging my code, and I imagine it's my fault rather than Unity's, but I'm curious about the two assertions mentioned above. Could someone tell me what condition causes them to be triggered?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    This assert would be triggered if a function is called before Awake has been called on that script.

    This could happen through SendMessage, StartCoroutine. Not sure maybe other functions too. Can't really tell why it happens without more context.
     
  3. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Is it possible for Update to be called before Awake for scripts which are added while the game is in progress? Also, would BroadcastMessage with DontRequireReceiver and no appropriate method on the script cause this message to appear?
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    It shouldn't be possible to trigger those asserts.
    If you want to, you can send the project and i will take a look at it.
     
  5. cblarsen

    cblarsen

    Joined:
    Mar 10, 2007
    Posts:
    266
    If you still haven't nailed this bug, I think I have isolated where it happens.

    I have just made an ultra-light bug report with a project demonstrating the issue. The summary starts off with "Update is called before Awake"

    When AddComponent is used to add a script in a function called from OnMouseDown/OnMouseUp, etc. Update will be called before Awake, and you will get an !m_DidAwake assertion in the log.

    If, on the other hand the same code had been called from within an Update function, the error would not occur.

    The cause of the problem is probably related to the calling order that you (Joachim) outline in this thread:

    http://forum.unity3d.com/viewtopic.php?t=2719

    My current workaround consists in tolerating the assert, and calling my own Init() function instead of relying on Awake/Start