Search Unity

[Help] A* Pathfinding - Threading - Sebastian Lague Tutorial

Discussion in 'Scripting' started by raveranor, May 15, 2020.

  1. raveranor

    raveranor

    Joined:
    Apr 9, 2020
    Posts:
    1
    Hi guys, I am following a Sebastian Lague tutorial on Pathfinding



    github (Sebastian Lague): https://github.com/SebLague/Pathfinding/tree/master/Episode 10 - threading/Assets/Scripts

    I made all tutorial steps, but when I've done everything, I checked the comments and found out that there is an issue with the call of the Thread.

    Then, I replaced this code:

    Code (CSharp):
    1. public static void RequestPath(PathRequest request)
    2.     {
    3.         ThreadStart threadStart = delegate
    4.         {
    5.             instance.pathfinding.FindPath(request, instance.FinishedProcessingPath);
    6.         };
    7.  
    8.         threadStart.Invoke(); // THIS LINE
    9.     }
    by this:

    Code (CSharp):
    1. public static void RequestPath(PathRequest request)
    2.     {
    3.         ThreadStart threadStart = delegate
    4.         {
    5.             instance.pathfinding.FindPath(request, instance.FinishedProcessingPath);
    6.         };
    7.  
    8.         Thread thread = new Thread(threadStart);
    9.         thread.Start();
    10.     }
    And then I got the following error:

    NullReferenceException: Object reference not set to an instance of an object
    Node.CompareTo (Node nodeToCompare) (at Assets/Scripts/Node.cs:48)
    Heap`1[T].SortUp (T item) (at Assets/Scripts/Heap.cs:94)
    Heap`1[T].UpdateItem (T item) (at Assets/Scripts/Heap.cs:35)
    Pathfinding.FindPath (PathRequest request, System.Action`1[T] callback) (at Assets/Scripts/Pathfinding.cs:61)
    PathRequestManager+<>c__DisplayClass5_0.<RequestPath>b__0 () (at Assets/Scripts/PathRequestManager.cs:40)
    System.Threading.ThreadHelper.ThreadStart_Context (System.Object state) (at <437ba245d8404784b9fbab9b439ac908>:0)
    System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) (at <437ba245d8404784b9fbab9b439ac908>:0)
    System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) (at <437ba245d8404784b9fbab9b439ac908>:0)
    System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state) (at <437ba245d8404784b9fbab9b439ac908>:0)
    System.Threading.ThreadHelper.ThreadStart () (at <437ba245d8404784b9fbab9b439ac908>:0)
    UnityEngine.<>c:<RegisterUECatcher>b__0_0(Object, UnhandledExceptionEventArgs)

    This error is in the Node class in the following method:

    Code (CSharp):
    1. public int CompareTo(Node nodeToCompare)
    2.     {
    3.         int compare = fCost.CompareTo(nodeToCompare.fCost); // THIS LINE
    4.         if (compare == 0)
    5.         {
    6.             compare = hCost.CompareTo(nodeToCompare.hCost);
    7.         }
    8.         return -compare;
    9.     }
    I searched if someone had the same issue but I didn't found.

    Please, someone can help-me. Thank You.
     
  2. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. Node.CompareTo (Node nodeToCompare) (at Assets/Scripts/Node.cs:48)
    To find a fix, try to understand what is going on?
    It does say at line 48 into the Node.cs file, the script is trying to get something from an object which is null itself...

    You could start adding the following to the top of the buggy line to see which one is null?

    Code (CSharp):
    1. if(fCost == null)
    2. {
    3. Debug.Log(<color=red>"fCost is null</color>");
    4. }
    5. if(nodeToCompare == null)
    6. {
    7. Debug.Log(<color=red>"nodeToCompareis null</color>");
    8. }
    9.  
     
    raveranor likes this.