• 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 zlong1 · Sep 30, 2018 at 10:53 AM · destroyerror messagetagdestroygameobject

The object of type 'Transform' has been destroyed but you are still trying to access it.

 using UnityEngine;
 using System.Collections;
 
 public class AlliedMovment : MonoBehaviour
 {
     Transform enemy;
     PlayerHealth playerHealth;
     EnemyHealth enemyHealth;
     UnityEngine.AI.NavMeshAgent nav;
 
     bool isDead;
 
 
     void Awake()
     {
         enemy = GameObject.FindGameObjectWithTag("Enemy").transform;
         nav = GetComponent<UnityEngine.AI.NavMeshAgent>();
     }
 
 
     void Update()
 
     {
         
         if (GameObject.FindGameObjectWithTag("Enemy") == isDead)
         {
             Destroy(enemy);
             enemy = GameObject.FindGameObjectWithTag("Enemy").transform;
             nav = GetComponent<UnityEngine.AI.NavMeshAgent>();
         }
         else
         {
             nav.SetDestination(enemy.position);
         }
     }
 }

Hello, I'm a student with the assignment to modify the Survival Shooter Tutorial. I thought it would be cool to make some of the units friends with the player and help it fight. To make this happen I'm trying to make a script that tells a friendly unit to seek out an enemy, fight it, and then find a new enemy to repeat the process.

I have been trying to figure this out for the past two days and have made little progress. It technically works, except after a unit kills an enemy it will just stand there and will not seek a new target. The console returns an 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. UnityEngine.Transform.get_position () AlliedMovment.Update () (at Assets/Scripts/Ally/AlliedMovment.cs:26)

"

I have tried to use Destroy(gameObject); to delete the reference, but I couldn't seem to implement it correctly. I also can't seem to figure out a way to tell the friendly unit to seek out the nearest enemy unit. They tend to chase what was closest to them at the time and chase it to the ends of the earth.

I have searched many topics with the same error message, but I have not had luck translating it into my own problem.

I clearly still have a lot to learn, so I was hoping if anyone could please explain what I'm doing wrong or suggest a better way to implement the code?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LCStark · Sep 30, 2018 at 11:31 AM

If you use Destroy(enemy), you are only destroying the Transform component, meaning the Enemy object is still there. Your code then finds this object and tries to use its transform, but since it has been destroyed, you're getting the error.

If you use Destroy(gameObject), you are passing the GameObject assigned to your AlliedMovment object, which would destroy the wrong object.

Try using Destroy(enemy.gameObject). This should get rid of the entire Enemy game object.

EDIT
Also, I think that the Destroy doesn't destroy the object immediately, so since you're searching for a new object immediately after "destroying" the previous one, you'll still probably find the same object. Try modifying your code so when you destroy your current enemy you set your enemy reference to null, and at the beginning of your Update function you add:

if (enemy == null) {
    // find new enemy
}
Comment
Add comment · Share
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

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

99 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 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 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 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 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

How to assign a color to a game object & destroy it when it collides with another game object of same Color 1 Answer

Destorying gameobjects on a network 0 Answers

Incorrect tag 1 Answer

Tagged Clones not destroyed by script 1 Answer

Network.Destroy and collision; destroying correct object. 1 Answer


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