!m_DidAwake assertions

I’m getting these assertions while attempting to add a script component to an existing object using GameObject.AddComponent():

!m_DidAwake (Filename: MonoBehaviour.cpp Line: 180)
!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?

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.

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?

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.

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