Problem with OnDestroy

When Child gameobject is destroyed , i add a component to his parent. the code like this:

void OnDestroy()
{
    Debug.Log("Destroy");
    this.myTransformparent.gameObject.AddComponent<GameOver>();
}

then i attach the script to the child.

  1. play the game.[29552-qq截图20140722170304.png|29552]

  2. nothing to do except to exit the game.
    [29553-qq截图20140722170521.png|29553]

  3. there are some error in the follow.

!IsDestroying() UnityEngine.GameObject:AddComponent() PlayerScript:OnDestroy() (at Assets/Scripts/PlayerScript.cs:121) batchDelete.reservedObjectCount >= batchDelete.objectCount.

any one can help me ?

Unity doesn’t allow you to add components in OnDestroy, because then when the game is shutting down more components are created. When the parent object is destroyed, its children are destroyed as well, at which point we try to add components to parent, which doesn’t give a nullreference, but the object IS already destroyed.

What you should do is call AddGameOver from the parent just after you Destroy the child. That way it will not be called when the game shuts down.