Search Unity

Weird problem about executing Awake methods

Discussion in 'Scripting' started by mahdiii, Dec 23, 2017.

  1. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    Hi. When I run the app,Awake method is executed and go inside InitializePanelDictionary function but when it reaches SetPanelData function, it is not executed and instead other awakes are executed!!



    Code (CSharp):
    1.  
    2.   [System.Serializable]
    3.     public class CPanelData
    4.     {
    5.         public CBasePanelView m_basePanel;
    6.         public CBasePanelView m_parentBasePanel;
    7.         public EPanelType m_panelType;
    8.     }
    9.  
    10.  
    11.  
    12.   [SerializeField]
    13.     private CPanelData[] m_panelData;
    14. public void InitializePanelDictionary()
    15.     {
    16.         m_panelDictionary = new Dictionary<EPanelType, CBasePanelView>(4 * m_panelData.Length); // multiply by 4 to avoid more collision
    17.         for (int i = 0; i < m_panelData.Length; i++)
    18.         {
    19. // m_panelData: it is an array
    20.             CBasePanelView basePanel = m_panelData[i].m_basePanel;
    21.             basePanel.SetPanelData(m_panelData[i].m_panelType, m_panelData[i].m_parentBasePanel.m_panelData);
    22.            
    23.             m_panelDictionary.Add(m_panelData[i].m_panelType, basePanel);
    24.         }
    25.     }
    26.     // Use this for initialization
    27.     protected override void Awake()
    28.     {
    29.         base.Awake();
    30.         InitializePanelDictionary();
    31.     }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    The method would run all the way through unless you are getting an error?
     
  3. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. CPanelManager.InitializePanelDictionary () (at Assets/Scripts/Manager/CPanelManager.cs:122)
    3. CPanelManager.Awake () (at Assets/Scripts/Manager/CPanelManager.cs:131)

    this line is 122
    basePanel.SetPanelData(m_panelData.m_panelType, m_panelData.m_parentBasePanel.m_panelData);

    I checked it basePanel is not null and is initialized correctly

    I found and got it thx
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, what about the other parts (the parameters) -- are those not null? Something on that line must be, to give you that error. That's also why you see other Awake methods being called, as that Awake throws an exception, doesn't continue, and other scripts go on...
     
    mahdiii likes this.