Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

NavMeshAgent a little problem! non static error

Discussion in 'Scripting' started by Nitzaproductions, Jan 27, 2013.

  1. Nitzaproductions

    Nitzaproductions

    Joined:
    Jan 10, 2013
    Posts:
    36
    Code (csharp):
    1.  
    2. if (NavMeshAgent.remainingDistance  <= 0) {
    3. playeranima.animation.Stop
    4. }
    5.  
    and it gives me an error so whats the problem i cant understand!
     
  2. zyzyx

    zyzyx

    Joined:
    Jul 9, 2012
    Posts:
    226
    You try to call remainingDistance from the class NavMeshAgent wich is not a static Field/Property. You need to get a NavMeshAgent object-reference by using GetComponent<NavMeshAgent>().
     
    Last edited: Jan 27, 2013
  3. Nitzaproductions

    Nitzaproductions

    Joined:
    Jan 10, 2013
    Posts:
    36
    so it hast to be like this ?
    Code (csharp):
    1. if GetComponent(NavMeshAgent.remainingDistance
     
  4. zyzyx

    zyzyx

    Joined:
    Jul 9, 2012
    Posts:
    226
    No. More something like this (C#):
    Code (csharp):
    1.  
    2. NavMeshAgent navmeshagent;
    3.  
    4. void Start()
    5. {
    6.     navmeshagent = GetComponent<NavMeshAgent>();
    7. }
    8.  
    9.  
    Now you have access to your NavMeshAgent's functions and properties.

    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.     if (navmeshagent.remainingDistance  <= 0) {
    5.  
    6.         ...
    7.  
    8.     }
    9. }
    10.  
     
  5. Nitzaproductions

    Nitzaproductions

    Joined:
    Jan 10, 2013
    Posts:
    36
    oooh good! thank you very much mate !!!