Search Unity

3 identical objects, event received only by 2

Discussion in 'Scripting' started by CG_Echtzeitschmiede, Apr 25, 2015.

  1. CG_Echtzeitschmiede

    CG_Echtzeitschmiede

    Joined:
    Feb 19, 2015
    Posts:
    93
    Hello,

    I have a canvas with three text objects in my scene. Each of the text objects uses the same script. In simplified form:
    Code (CSharp):
    1. public class TextLevelData : MonoBehaviour
    2. {
    3.     void OnEnable()
    4.     {
    5.         SaveLoad.OnLoadCompleted += UpdateText;
    6.     }
    7.  
    8.     void OnDisable()
    9.     {
    10.         SaveLoad.OnLoadCompleted -= UpdateText;
    11.     }
    12.  
    13.     void UpdateText()
    14.     {
    15.         print (gameObject.name);
    16.      }
    17. }
    18.  
    And the class with the event:
    Code (CSharp):
    1. public class SaveLoad
    2. {
    3.     public delegate void OnLoadCompletedEvent();
    4.     public static event OnLoadCompletedEvent OnLoadCompleted;
    5.  
    6.     static public void Load()
    7.     {        
    8.            // here some code
    9.             if (OnLoadCompleted != null)
    10.             {
    11.                 Debug.Log(OnLoadCompleted);
    12.                 OnLoadCompleted();
    13.             }
    14.     }
    15. }
    16.  
    Only two of the objects print their name. If I remove the script from the object that doesn't work and add the script anew, it works. But only until I reopen the scene. Then the object stops working again.
    What's more, I have the exact same canvas in another scene, and in that other scene, another of the three objects doesn't receive the event while the other two work.

    What is going on with this??
     
    Last edited: Apr 25, 2015
  2. CG_Echtzeitschmiede

    CG_Echtzeitschmiede

    Joined:
    Feb 19, 2015
    Posts:
    93
    I printed out some realtimeSinceStartup times, and it turns out that the execution order is messed up.
    I call SaveLoad.Load() from my GameController object's OnLevelWasLoaded(), firing the event. However, for one of the three objects, its OnEnable() is called AFTER OnLevelWasLoaded(). Even its Awake() is called AFTER OnLevelWasLoaded().
    Is this supposed to be normal?