Search Unity

A* Pathfinding Project question in inheritance

Discussion in 'Scripting' started by BTippen, Jul 12, 2020.

  1. BTippen

    BTippen

    Joined:
    Jul 9, 2019
    Posts:
    6
    I am using the A* Pathfinding Project but getting a null reference to the IAstarAI when I GetComponent in the top level of inheritance. Same code works when I use it in the lower level. Both scripts are attached to the gameobject.

    Anyone got any ideas why this happens?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,694
    Some notes on how to fix a NullReferenceException in Unity3D (also Unassigned Reference errors):

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.
     
  3. BTippen

    BTippen

    Joined:
    Jul 9, 2019
    Posts:
    6
    I found the solution. I changed the start() to Awake(). I am aware the Awake() loads later but still not sure why this fixed the issue.
     
  4. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    Awake is invoked first.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,694
    This is incorrect. I suggest you review the Unity Monobehavior event lifecycle chart. It is critical to your successfully engineering solutions within the Unity3D API and framework.

    Here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html