• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SilenceDoGood · Apr 26, 2014 at 12:38 AM · gameobjectnavmeshnavagent

Issues with NavMeshAgent being destoryed

I'm having an issue with the unintended disposal of the NavMeshAgent and its Pathing information. I have an event driven guard script which controls where the guard is pathing too. I have two states for this behavior, Normal and Alert. All behaviors function as intended in the first scene. When I load the next scene the guards function perfectly in the normal state, and then chase the player as intended in alert state, but when the state changes back to normal I get this error:

MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.Your script should either check if it is null or you should not destroy the object.

If I try to access the NavMeshAgent after this I get a similar error.I'm wondering why the agent and PathCommand[] variable get destroyed in the second scene after changing states. I've been troubleshooting this problem for 5 plus hours and any suggestions could be helpful.

Here is the PathCommands Class used for the path nodes.

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class PathCommand
 {
     public Transform node;
     public bool endAfterDuration;
     public float duration;
 }
 

Here is the NavMeshPathTracker Class on the Guard

 using UnityEngine;
 using System.Collections;
 
 public class NavMeshPathTracker : MonoBehaviour
 {
     private EventManager.States state_;
 
     public NavMeshAgent agent; //set this in start
     public PathCommand[] path;
     public GameObject player;
 
     private float behaviorTime;
     private int nodeIndex = 0;
     private float timeFollowing; //this counts up on update or use game ticks (difference between frame started following and current frame)
     private Transform currentTarget;
 
 
     //set agent to this object's navMeshAgent
 
     void Awake ()
     {
         timeFollowing = 0;
         agent.SetDestination(path[nodeIndex].node.transform.position);
 
         EventManager.OnNormal += OnNormal;
         EventManager.OnCaution += OnCaution;
         EventManager.OnAlert += OnAlert;
         EventManager.OnVictory += OnVictory;
         EventManager.OnDefeat += OnDefeat;
     }
 
     void Update()
     {
         switch (state_)
         {
             case EventManager.States.STATE_NORMAL:
             AnimationControl ();
             timeFollowing += Time.deltaTime;
             
             if ((!path[nodeIndex].endAfterDuration && agent.remainingDistance == 0) || (path[nodeIndex].endAfterDuration && timeFollowing > path[nodeIndex].duration))
             {
                 //Run commands, make another class to handle behaviors
                 if(path.Length > 1)
                 {
                     TrackToNext();
                 }
             }
             break;
             
             case EventManager.States.STATE_CAUTION:
             //Do Caution stuff
             break;
             
             case EventManager.States.STATE_ALERT:
                 agent.speed = 50;
                 animation.CrossFade("run");
                 agent.SetDestination(player.transform.position);
             break;
             
             case EventManager.States.STATE_VICTORY:
             //Do Victory Stuff
             break;
             
             case EventManager.States.STATE_DEFEAT:
             //Do Defeat Stuff
             break;
         }
     }
 
     void TrackToNext()
     {
         timeFollowing = 0;
         nodeIndex++;
         if(nodeIndex >= path.Length)
         {
             nodeIndex = 0;
         }
         agent.SetDestination(path[nodeIndex].node.transform.position);
     }
 
     void ChasePlayer()
     {
         agent.speed = 50;
         animation.CrossFade("run");
         agent.SetDestination(player.transform.position);
     }
 
     void AnimationControl()
     {
         if(agent.remainingDistance < 2.0F)
         {
             animation.CrossFade("idle", 0.5F);
         }
         else
         {
             animation.CrossFade("walk", 0.5F);
         }
     }
 
     public void OnAlert()
     {
         if(state_ != EventManager.States.STATE_ALERT)
         {
             state_ = EventManager.States.STATE_ALERT;
         }
     }
     
     public void OnCaution()
     {
         if(state_ != EventManager.States.STATE_ALERT && state_ != EventManager.States.STATE_CAUTION)
         {
             state_ = EventManager.States.STATE_CAUTION;
         }
 
     }
     
     public void OnVictory()
     {
         state_ = EventManager.States.STATE_VICTORY;
     }
     
     public void OnNormal()
     {    
         TrackToNext();
         state_ = EventManager.States.STATE_NORMAL;
     }
     
     public void OnDefeat()
     {
         state_ = EventManager.States.STATE_DEFEAT;
     }
 }

 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

NavMeshAgent not fully reaching destination, thus it won't delete itself when it gets there 1 Answer

How to save a gameObjects on a prefab and variables with a script? 1 Answer

How to get a variable value from another script(C#)? 1 Answer

C# Rotate GameObjects Regardless of List Size 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges