• 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 kringler · Dec 09, 2013 at 06:37 AM · transformdestroygameobject

Problem with destroying Transforms

I'm trying to make a very basic RTS level. The 'Enemy' is an object to make the player units move towards it and then destroys it when they touch it. The problem is that once they destroy it the game gives me an error "MissingReferenceException: The object of type 'transform' has been destroyed but you are still trying to access it". I tried having a backup copy of the Enemy object floating in the sky-box somewhere but then the player unit just floats up and goes after that, ignoring the Enemy objects that the player spawns.

How do I make the player unit sit there and wait for the player to spawn an Enemy object for it to move to instead of crashing instantly or chasing after ones thousands of miles away.

This is the script that the player unit is using and spawns on scene start.

     var MoveSpeed : float = 2;
     var Enemy : Transform;
     var MaxDist = 10;
     var MinDist = 5;
      
     function Update ()
     {
     transform.LookAt(Enemy);
     if(Vector3.Distance(transform.position,Enemy.position) >= MinDist)
         {
         transform.position += transform.forward*MoveSpeed*Time.deltaTime;
         }
     }
     
     function OnCollisionEnter(col : Collision) {
     if(col.gameObject.tag == "Enemy_Goal") {
         Application.LoadLevel("level2");
     }
     
     if(col.gameObject.tag == "Enemy") {
         Destroy(col.gameObject);
     }
     
 }
Comment
Add comment · Show 1
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
avatar image Santosh Patil · Dec 09, 2013 at 07:02 AM 1
Share

Hey kringler, regarding your "$$anonymous$$issingReferenceException" error change Update function with following code.

function Update () { if(Enemy != null) { transform.LookAt(Enemy); if(Vector3.Distance(transform.position,Enemy.position) >= $$anonymous$$inDist) { transform.position += transform.forward*$$anonymous$$oveSpeed*Time.deltaTime; } } }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hamstar · Dec 09, 2013 at 06:28 PM

Is there just one enemy? Santosh gave the answer basically. You only want to follow the Enemy if the Game Object exists. As soon as you destroy the Enemy object there is nothing to follow.

You need to assign the Enemy transform reference again after destroying and respawning it. This could be done from another script or something like this:

 var MoveSpeed : float = 2;
 var Enemy : Transform;
 var MaxDist = 10;
 var MinDist = 5;
  
 function Update ()
 {
   // see if enemy has been spawned if we don't have reference to it
   if(Enemy == null) {
     GameObject enemyObj = GameObject.FindWithTag("Enemy");
     if(enemyObj != null) {
       Enemy = enemyObj.transform;
     }
   }
   // we already have a reference to the enemy
   else {
     transform.LookAt(Enemy);
     if(Vector3.Distance(transform.position,Enemy.position) >= MinDist)
     {
       transform.position += transform.forward*MoveSpeed*Time.deltaTime;
     }
   }
 }
  
 function OnCollisionEnter(col : Collision) {
   if(col.gameObject.tag == "Enemy_Goal") {
     Application.LoadLevel("level2");
   }
  
   if(col.gameObject.tag == "Enemy") {
     Destroy(col.gameObject);
     // make reference null so player stops trying to follow it
     Enemy = null;
   } 
 }
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

18 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

Related Questions

respawn delay 2 Answers

Difference between transform.forward and transform.position.z? 1 Answer

What is equivalent to Transform.up when moving and rotating the RigidBody2D component instead? 1 Answer

Calculating translation speed? 1 Answer

Convert from Image plane to World plane. Inverse perspective Mapping 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