Search Unity

Multiple Instantiated NavMeshAgent

Discussion in 'Navigation' started by pixelone, Mar 1, 2015.

  1. pixelone

    pixelone

    Joined:
    Apr 16, 2010
    Posts:
    157
    For reasons my code is not working as it I thought it would.

    I am instantiating multiple NPCs with the script below. This script is attached to my npc prefab. Only the first instantiated npc does the patrolling. The ones that are instantiated after does nothing. They do not move or patrol the waypoints. It's as if the path or destination is not set or you cannot have multiple npc with NavMeshAgents (Not true). Can someone help me with this. See my code below.

    Code (CSharp):
    1.  
    2.     public float patrolWaitTime = 5f;
    3.     public float patrolSpeed = 2f;
    4.     public Transform[] patrolWayPoints;
    5.     private int curWaypoint = 0;
    6.     private int maxWaypoint;
    7.     public float minWayPointDistance = 0.1f;
    8.  
    9.    
    10.    
    11.     void Awake(){
    12.         bones = GameObject.FindGameObjectWithTag ("Bones");
    13.         anim = bones.GetComponent<Animator> ();
    14.         nm = bones.GetComponent<NavMeshAgent> ();
    15.  
    16.         maxWaypoint = patrolWayPoints.Length - 1;
    17.  
    18.     }
    19.    
    20.     // Use this for initialization
    21.     void Start () {
    22.         if (bones == null)
    23.             Debug.Log ("Where is the enemy named Bones?");
    24.         if (anim == null)
    25.             Debug.Log ("Bones needs to have an Animator component");
    26.         if (nm == null)
    27.             Debug.Log ("Please attach the NavMeshAgent on Bones");
    28.  
    29.  
    30.         player = GameObject.FindGameObjectWithTag ("Player");
    31.         playerhealth = player.GetComponent<PlayerHealth> ();
    32.         boltSpawn = GameObject.Find ("BoltSpawn");
    33.    
    34.     }
    35.  
    36.         void Update(){
    37.          Patrolling ();
    38. }
    39.  
    40.     void Patrolling(){
    41.                
    42.                 Vector3 tempLocalPosition;
    43.                 Vector3 temWaypointPosition;
    44.  
    45.                 //Agents position
    46.                 tempLocalPosition = transform.position;
    47.                 tempLocalPosition.y = 0f;
    48.  
    49.                 //Current waypoints
    50.                 temWaypointPosition = patrolWayPoints [curWaypoint].position;
    51.                 temWaypointPosition.y = 0f;
    52.  
    53.                 //Is distance between the agent and the current waypoint within mid waypoint distance
    54.                 if (Vector3.Distance (tempLocalPosition, temWaypointPosition) <= minWayPointDistance) {
    55.                         if (curWaypoint == maxWaypoint)
    56.                                 curWaypoint = 0;
    57.                         else
    58.                                 curWaypoint++;
    59.                 }
    60.  
    61.                 nm.SetDestination (patrolWayPoints [curWaypoint].position);
    62.                 nm.speed = patrolSpeed;
    63.                 anim.SetFloat ("Speed", patrolSpeed);
    64.  
    65.         }
     
  2. pixelone

    pixelone

    Joined:
    Apr 16, 2010
    Posts:
    157
    Any help is appreciated
     
  3. Jakob_Unity

    Jakob_Unity

    Joined:
    Dec 25, 2011
    Posts:
    269
    In awake and start you scan the scene for a (one) game-object that has the tag "Bones".
    If this script sits on your prefab - you should query the components of the local game object - i.e.:

    nm = GetComponent<NavMeshAgent>();

    - leaving out the 'bones' variable
     
  4. wethecom

    wethecom

    Joined:
    Jun 24, 2015
    Posts:
    75
    im not sure if this will help you but it when i instantiated a NPC in range of a enemy it fired the navmesh.setdestination
    and gave a error "SetDestination" can only be called on an active agent" that has been placed on a NavMesh"
    so i put in start navmeshagent.enabled false; and in update navmeshagent enabled = true; and it solved my issues...see if it works for you