Search Unity

Resolved NavMeshAgent.CalculatePath gives null reference exception

Discussion in 'Navigation' started by nikofon007, Sep 10, 2020.

  1. nikofon007

    nikofon007

    Joined:
    Sep 10, 2020
    Posts:
    38
    I have this error when I'm running my script:
    NullReferenceException: Object reference not set to an instance of an object
    UnityEngine.AI.NavMeshAgent.CalculatePath (UnityEngine.Vector3 targetPosition, UnityEngine.AI.NavMeshPath path) (at < c7d8111c68e74de4b24f0d8ed8598f1b >:0)

    I've tried asking on unit answers, noone responded (https://answers.unity.com/questions/1770312/navmeshagentcalculatepath-keeps-getting-null-refer.html).

    I've used the code provided by the user Shudrikn from the link below to calculate NavMeshPath cost https://forum.unity.com/threads/cos...95.306305390.1599944264-1849265996.1585780230 . But I changed methods from being static to normal cause I have multiple objects that uses this function separatly.

    I found this on unity answers, but it doesn't seem to help cause its about js and I'm using c#: https://answers.unity.com/questions/301736/navmeshagentcalculatepath-keep-getting-nullreferen.html

    Pls, help, this is essential for my project and I don't know what to do.
     
    Last edited: Sep 13, 2020
  2. Skynet766

    Skynet766

    Joined:
    Sep 13, 2020
    Posts:
    22
    If you comment out the CalculatePathCost() line, does the null reference error still occur?

    Also maybe CalculatePath() does something to the path which makes it impossible to re-use.
     
  3. nikofon007

    nikofon007

    Joined:
    Sep 10, 2020
    Posts:
    38
    it still occurs if I comment out that function + I used this function before and it worked (but not as intended) I think it all started with me adding dictionary.
     
  4. Skynet766

    Skynet766

    Joined:
    Sep 13, 2020
    Posts:
    22
    What happens if you instantiate a new NavMeshPath inside the foreach instead of outside?
     
  5. nikofon007

    nikofon007

    Joined:
    Sep 10, 2020
    Posts:
    38
    still same, it returns error even if I call agent.CalculatePath() outside this function (in the start method for example)
     
  6. nikofon007

    nikofon007

    Joined:
    Sep 10, 2020
    Posts:
    38
    ok, actually I'm stupid. I had another navmeshpath variable that I didn't instantiate so it returned the error when I tried to assign a valu to it. It's strange that unity don't mention the name of the variable that is "empty".