Search Unity

Game not working after update

Discussion in 'Navigation' started by tilledflame645, Mar 6, 2015.

  1. tilledflame645

    tilledflame645

    Joined:
    Mar 6, 2015
    Posts:
    1
    I updated a scene i was working on in 4.4 unity to unity 5 and my enemy ai wont work the characters just stand there but if you walk past them they try to shoot you this is my ai i get multiple errors

    "SetDestination" can only be called on an active agent that has been placed on a NavMesh.
    UnityEngine.NavMeshAgent:set_destination(Vector3)
    DoneEnemyAI:patrolling() (at Assets/Done/DoneScripts/EnemyScripts/DoneEnemyAI.cs:123)
    DoneEnemyAI:Update() (at Assets/Done/DoneScripts/EnemyScripts/DoneEnemyAI.cs:49)"

    "GetRemainingDistance" can only be called on an active agent that has been placed on a NavMesh.
    UnityEngine.NavMeshAgent:get_remainingDistance()
    DoneEnemyAI:patrolling() (at Assets/Done/DoneScripts/EnemyScripts/DoneEnemyAI.cs:100)
    DoneEnemyAI:Update() (at Assets/Done/DoneScripts/EnemyScripts/DoneEnemyAI.cs:49)"




    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DoneEnemyAI : MonoBehaviour
    5. {
    6.     public float patrolSpeed = 2f;                            // The nav mesh agent's speed when patrolling.
    7.     public float chaseSpeed = 5f;                            // The nav mesh agent's speed when chasing.
    8.     public float chaseWaitTime = 5f;                        // The amount of time to wait when the last sighting is reached.
    9.     public float patrolWaitTime = 1f;                        // The amount of time to wait when the patrol way point is reached.
    10.     public Transform[] patrolWayPoints;                        // An array of transforms for the patrol route.
    11.    
    12.    
    13.     private DoneEnemySight enemySight;                        // Reference to the EnemySight script.
    14.     private NavMeshAgent nav;                                // Reference to the nav mesh agent.
    15.     private Transform player;                                // Reference to the player's transform.
    16.     private DonePlayerHealth playerHealth;                    // Reference to the PlayerHealth script.
    17.     private DoneLastPlayerSighting lastPlayerSighting;        // Reference to the last global sighting of the player.
    18.     private float chaseTimer;                                // A timer for the chaseWaitTime.
    19.     private float patrolTimer;                                // A timer for the patrolWaitTime.
    20.     private int wayPointIndex;                                // A counter for the way point array.
    21.    
    22.    
    23.     void Awake ()
    24.     {
    25.         // Setting up the references.
    26.         enemySight = GetComponent<DoneEnemySight>();
    27.         nav = GetComponent<NavMeshAgent>();
    28.         player = GameObject.FindGameObjectWithTag(DoneTags.player).transform;
    29.         playerHealth = player.GetComponent<DonePlayerHealth>();
    30.         lastPlayerSighting = GameObject.FindGameObjectWithTag(DoneTags.gameController).GetComponent<DoneLastPlayerSighting>();
    31.     }
    32.    
    33.    
    34.     void Update ()
    35.     {
    36.         // If the player is in sight and is alive...
    37.         if(enemySight.playerInSight && playerHealth.health > 0f)
    38.             // ... shoot.
    39.             Shooting();
    40.        
    41.         // If the player has been sighted and isn't dead...
    42.         else if(enemySight.personalLastSighting != lastPlayerSighting.resetPosition && playerHealth.health > 0f)
    43.             // ... chase.
    44.             Chasing();
    45.        
    46.         // Otherwise...
    47.         else
    48.             // ... patrol.
    49.             Patrolling();
    50.     }
    51.    
    52.    
    53.     void Shooting ()
    54.     {
    55.         // Stop the enemy where it is.
    56.         nav.Stop();
    57.     }
    58.    
    59.    
    60.     void Chasing ()
    61.     {
    62.         // Create a vector from the enemy to the last sighting of the player.
    63.         Vector3 sightingDeltaPos = enemySight.personalLastSighting - transform.position;
    64.        
    65.         // If the the last personal sighting of the player is not close...
    66.         if(sightingDeltaPos.sqrMagnitude > 4f)
    67.             // ... set the destination for the NavMeshAgent to the last personal sighting of the player.
    68.             nav.destination = enemySight.personalLastSighting;
    69.        
    70.         // Set the appropriate speed for the NavMeshAgent.
    71.         nav.speed = chaseSpeed;
    72.        
    73.         // If near the last personal sighting...
    74.         if(nav.remainingDistance < nav.stoppingDistance)
    75.         {
    76.             // ... increment the timer.
    77.             chaseTimer += Time.deltaTime;
    78.            
    79.             // If the timer exceeds the wait time...
    80.             if(chaseTimer >= chaseWaitTime)
    81.             {
    82.                 // ... reset last global sighting, the last personal sighting and the timer.
    83.                 lastPlayerSighting.position = lastPlayerSighting.resetPosition;
    84.                 enemySight.personalLastSighting = lastPlayerSighting.resetPosition;
    85.                 chaseTimer = 0f;
    86.             }
    87.         }
    88.         else
    89.             // If not near the last sighting personal sighting of the player, reset the timer.
    90.             chaseTimer = 0f;
    91.     }
    92.  
    93.    
    94.     void Patrolling ()
    95.     {
    96.         // Set an appropriate speed for the NavMeshAgent.
    97.         nav.speed = patrolSpeed;
    98.        
    99.         // If near the next waypoint or there is no destination...
    100.         if(nav.destination == lastPlayerSighting.resetPosition || nav.remainingDistance < nav.stoppingDistance)
    101.         {
    102.             // ... increment the timer.
    103.             patrolTimer += Time.deltaTime;
    104.            
    105.             // If the timer exceeds the wait time...
    106.             if(patrolTimer >= patrolWaitTime)
    107.             {
    108.                 // ... increment the wayPointIndex.
    109.                 if(wayPointIndex == patrolWayPoints.Length - 1)
    110.                     wayPointIndex = 0;
    111.                 else
    112.                     wayPointIndex++;
    113.                
    114.                 // Reset the timer.
    115.                 patrolTimer = 1;
    116.             }
    117.         }
    118.         else
    119.             // If not near a destination, reset the timer.
    120.             patrolTimer = 0;
    121.        
    122.         // Set the destination to the patrolWayPoint.
    123.         nav.destination = patrolWayPoints[wayPointIndex].position;
    124.     }
    125. }
    126.  
     
  2. MongoBongo

    MongoBongo

    Joined:
    Dec 18, 2014
    Posts:
    5
    Was having the same problem, cuz I was using an AI loosely based off the same Stealth Project code.

    Problem appears to be the nav.Stop() function. With Unity4 when you set a new destination the NavAgent would start back up. But in Unity5 it seems that Stop means completely stopped, and won't do anything else until you use the.... nav.Resume() function. So you'll need to go through and add Resume to the Chase and Patrol routines.

    At least that's the solution I've found and what I've managed to reason together. It may or may not be entirely correct as I am no C# guru yet. But it does seem to work.
     
  3. Grhyll

    Grhyll

    Joined:
    Oct 15, 2012
    Posts:
    119
    Did you rebake the navmesh of your scene after the update?
     
  4. Jakob_Unity

    Jakob_Unity

    Joined:
    Dec 25, 2011
    Posts:
    269
    Ugraded projects (pre-5) need rebaking of navmesh - if used. You should see an error-message about that.
     
  5. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    I'm running into this issue as well. I did re-bake the nav mesh. It think .Stop() had its functionality change but this was not updated in the docs.
     
  6. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    Just verified. Spent many hours trying to figure this out, your forum post saved me.

    .Stop() does not work as it did previously. If you set a new path the agent it will no longer auto resume. This should be updated in the documentation.

    Isn't there suppose to be a way to send unity a doc issue notification? I can't find it.
     
  7. Jakob_Unity

    Jakob_Unity

    Joined:
    Dec 25, 2011
    Posts:
    269
  8. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    No worries. Must have missed it. Just didn't want others to spend the same time if I was missing.
     
  9. yummybrainz

    yummybrainz

    Joined:
    Jan 14, 2014
    Posts:
    69
    Is this still an issue? I have a project only built in Unity 5 getting this error.

    This happens when loading a new scene the AI if not destroyed throws this message and breaks everything. In one scene to fix I had to teleport my player and delete all AI out of range before loading the next scene. This does not seem practical though as now I am having this same error happen when leaving the menu scene which has AI.

    Current Unity version is 5.4.0
     
  10. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    "Stop" can only be called on an active agent that has been placed on a NavMesh.
    UnityEngine.AI.NavMeshAgent:Stop()


    When I kill the object, unity tries to run the Stop.NavMesh code
    Code (JavaScript):
    1. agent.Stop();
    and gives the above error. I tried using
    Code (JavaScript):
    1. agent != null
    and other things, but unity keeps trying to read that line and errors it. :mad:

    Can we not give up on this? It's breaking my game.

    WORK AROUND: Due to not being able to condition-out the stop nav code on instance destruction, I had to tourniquet it by using a return command before the code. So set up any conditions you need and simply pass 'return' before the offending dodgy navmesh code can error out in the first place.
     
    Last edited: Jan 7, 2017