Search Unity

Help with "Object reference not set to an instance of an object" problem

Discussion in 'Scripting' started by esra01, May 23, 2019.

  1. esra01

    esra01

    Joined:
    May 23, 2019
    Posts:
    3
    Hi I'm pretty new to unity and encounter this problem

    NullReferenceException: Object reference not set to an instance of an object
    PathRequestManager.RequestPath (Vector3 pathStart, Vector3 pathEnd, System.Action`2 callback) (at Assets/PathRequestManager.cs:26)
    Unit.Start () (at Assets/Unit.cs:15)

    And here's my code
    Code (CSharp):
    1. public class PathRequestManager : MonoBehaviour
    2. {
    3.  
    4.     Queue<PathRequest> pathRequestQueue = new Queue<PathRequest>();
    5.     PathRequest currentPathRequest;
    6.  
    7.     static PathRequestManager instance;
    8.     Pathfinding pathfinding;
    9.  
    10.     bool isProcessingPath;
    11.  
    12.     void Awake()
    13.     {
    14.         instance = this;
    15.         pathfinding = GetComponent<Pathfinding>();
    16.     }
    17.  
    18.     public static void RequestPath(Vector3 pathStart, Vector3 pathEnd, Action<Vector3[], bool> callback)
    19.     {
    20.         PathRequest newRequest = new PathRequest(pathStart, pathEnd, callback);
    21.         instance.pathRequestQueue.Enqueue(newRequest);
    22.         instance.TryProcessNext();
    23.     }
    24.  
    25.     void TryProcessNext()
    26.     {
    27.         if (!isProcessingPath && pathRequestQueue.Count > 0)
    28.         {
    29.             currentPathRequest = pathRequestQueue.Dequeue();
    30.             isProcessingPath = true;
    31.             pathfinding.StartFindPath(currentPathRequest.pathStart, currentPathRequest.pathEnd);
    32.         }
    33.     }
    And for the unit class
    Code (CSharp):
    1. public class Unit : MonoBehaviour
    2. {
    3.  
    4.     public Transform target;
    5.     float speed = 20;
    6.     Vector3[] path;
    7.     int targetIndex;
    8.  
    9.     void Start()
    10.     {
    11.         PathRequestManager.RequestPath(transform.position, target.position, OnPathFound);
    12.     }
    13.  
    14.     public void OnPathFound(Vector3[] newPath, bool pathSuccessful)
    15.     {
    16.         if (pathSuccessful)
    17.         {
    18.             path = newPath;
    19.             targetIndex = 0;
    20.             StopCoroutine("FollowPath");
    21.             StartCoroutine("FollowPath");
    22.         }
    23.     }
    I'm pretty stumped since the object should have been referenced at that point.
     

    Attached Files:

  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Can you be specific about what line 26 is in PathRequestManager.cs? But my guess is that "instance" is null, meaning that you've called RequestPath before Awake has had a chance to run, or that the GameObject that PathRequestManager is attached to has been destroyed before RequestPath has run.
     
  3. esra01

    esra01

    Joined:
    May 23, 2019
    Posts:
    3
    ah im sorry the line 26 is this one

    Code (CSharp):
    1.  public static void RequestPath(Vector3 pathStart, Vector3 pathEnd, Action<Vector3[], bool> callback)
    2.     {
    3.         PathRequest newRequest = new PathRequest(pathStart, pathEnd, callback);
    4.         instance.pathRequestQueue.Enqueue(newRequest);  //line 26
    5.         instance.TryProcessNext();
    6.     }
    And that's where im stuck. Since i used Awake on PathRequestManager.cs and i used Start on Unit Class so the pathrequestmanager should start first(?) before it got called by Unit class.

    Is there any way to ensure the PathRequestManager run before anything else?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Awake should be called first, but if the GameObject PathRequestManager.cs is attached to is not active, or if it is a prefab you are instantiating, then Unit.Start() could get called first. Add a check before line 26 to verify that instance being null is the issue.

    I'd also add Debug.Log statements to Unit.Start and PathRequestManager.Awake so you can see what order they are actually being called. It is also possible that Awake is getting called first, but you have code somewhere else making instance null.

    If instance isn't null, something could have made your queue reference null then.
     
  5. esra01

    esra01

    Joined:
    May 23, 2019
    Posts:
    3
    Ahhh i see i got it solved. It's because i deleted the thing that the pathrequestmanager is attached to after i did some rework on my code.

    Thanks man!