• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by husainh00 · Jun 28, 2013 at 05:59 AM · errortransform

still trying to access a destroyed transform...

hi i'm sorry if its a stupid question but i only asked because i couldn't solve it.

the 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.Component.get_transform () (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineComponent.cs:21) RedSolder.WhereToLook () (at Assets/RedSolder.js:90) RedSolder.Update () (at Assets/RedSolder.js:26)

i know what the error means i just cant find out what part of the script its coming from, any help is appreciated

 #pragma strict
 var Health:float=10;
 var speed:float=1;
 var range:float=5;
 private var hit : RaycastHit;
 
 var wheretolook:Transform;
 
 private var lastUpdate:float=0;
 private var EnemyBase:GameObject;
 private var RotateSpeed:float=10;
 
 function Start () {
     
         EnemyBase=GameObject.FindWithTag("BlueBase");
         wheretolook=EnemyBase.transform;
 
 }
 
 function Update () {
         //defining the closest enemy few times a second
         var ClosestEnemy = Vector3.Distance(FindClosestEnemy().transform.position, transform.position);
         
         //always running these functions so the variables arnt null
         FindClosestEnemy();
         WhereToLook();
 
         
         Debug.DrawRay(transform.position , transform.forward * 8 , Color.white);
         //this is to avoid colliding and going throigh team mates
         if(Physics.Raycast(transform.position, transform.forward,hit, range)){
                 
                     //if its a team mate then rotate away
                      if(hit.transform.tag =="Red"){
                         RotateAway();
                     }
 
         }
         
 
         
         //if the enemy is closer then 8
          if(ClosestEnemy<8) {
          //and if hes in range then attack
             if(ClosestEnemy<=range){
                 Attack();
             }
             //if not in range then move in more
             else{
                 transform.Translate(Vector3.forward * speed * Time.smoothDeltaTime);
             }
             //this makes sure that we have something to look at        
             wheretolook=FindClosestEnemy().transform;
             
             
         }//if the enmey is not closer then 8 then move into their base
         else{
             transform.Translate(Vector3.forward * speed * Time.smoothDeltaTime);
             wheretolook=EnemyBase.transform;
         }
         //if we have nothing to look at then make the base the primary target
         if(wheretolook ==null){
             wheretolook=EnemyBase.transform;
             transform.Translate(Vector3.forward * speed * Time.smoothDeltaTime);
         }
         //this is self explanatory
         if(Health<=0){
         Destroy(this.gameObject);
         }
         
 }
 
 function RotateAway(){
 transform.Rotate(Vector3.up, -170*RotateSpeed* Time.smoothDeltaTime);
 }
 
 
 function WhereToLook(){
 
 var rotate = Quaternion.LookRotation(wheretolook.transform.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime *0.4);
 
 
 }
 
 function Attack(){
 
   if(Time.time - lastUpdate >= 1f){
   
         FindClosestEnemy().SendMessageUpwards("ApplyDamage", 1, SendMessageOptions.DontRequireReceiver);
         
         lastUpdate = Time.time;
     }
     
 }
 
 //thi is where other enemys call in to give damage
 function ApplyDamage (damage : float) {
     Health-=damage;
 }
     
 
         
 function FindClosestEnemy () : GameObject {
     
       // Find all game objects with tag Enemy
     var gos : GameObject[];
     gos = GameObject.FindGameObjectsWithTag("Blue"); 
     
       
         var closest : GameObject; 
         var distance = Mathf.Infinity; 
         var position = transform.position; 
         // Iterate through them and find the closest one
         for (var go : GameObject in gos)  { 
             var diff = (go.transform.position - position);
             var curDistance = diff.sqrMagnitude; 
             if (curDistance < distance) { 
                 closest = go; 
                 distance = curDistance; 
             } 
         } 
         return closest;    
     }
Comment
Bunny83

People who like this

1 Show 2
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 Em3rgency · Jun 28, 2013 at 06:26 AM 0
Share

Each error comes with some numbers, the row and column of where its at in the script. So give us those first, and then we can analyze the code and check WHY its throwing that.

avatar image Bunny83 · Jun 28, 2013 at 06:52 AM 0
Share

Some more points:

  • In line 21 FindClosestEnemy could return null if no enemy is found, so FindClosestEnemy().transform will cause a problem in this case.

  • In line 25 you call FindClosestEnemy but you ignore it's return value so it's useless.

  • You should save the enemy reference you get from FindClosestEnemy in a member variable and check if you actually have an enemy.

  • In WhereToLook you should check if "wheretolook" is null.

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Jun 28, 2013 at 06:39 AM

This should give you all information you need:

 "RedSolder.WhereToLook () (at Assets/RedSolder.js:90) RedSolder.Update () (at Assets/RedSolder.js:26)"

So the exception has been caught in Update at line 26. There you call the function WhereToLook. Furthermore it tells you that the error was inside the function WhereToLook in line 90, however it seems you edited the script because line 90 is outside the function. So i guess the actual line is line 80 because the error comes from:

 UnityEngine.Component.get_transform

So your problem comes from this statement:

 wheretolook.transform.....

because the object referenced by wheretolook doesn't exist anymore, therefore you can't get the transform component from it.

which is the getter of a components transform property

Comment
husainh00

People who like this

1 Show 1 · 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
avatar image husainh00 · Jun 28, 2013 at 06:49 AM 0
Share

oh ya i get it, thats for ur help.... to get ride of the error if anyone is interested.. i added if(wheretolook == null){ wheretolook = EnemyBase.transform; }

avatar image

Answer by PangeaMMO · Jun 28, 2013 at 06:30 AM

Have you tried changing

 Destroy(this.gameObject);

to

 Destroy(gameObject);
Comment
husainh00

People who like this

1 Show 2 · 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
avatar image husainh00 · Jun 28, 2013 at 06:34 AM 0
Share

ya but it didn't work. thanks for ur help

avatar image Bunny83 · Jun 28, 2013 at 06:40 AM 0
Share

Those two lines are actually exactly the same ;)

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

Argument is out of range in array 1 Answer

The nested type `TransformDirection' does not exist in the type `UnityEngine.Transform' 1 Answer

What is wrong with this code for adjusting the walls and players to match the screen size? 1 Answer

Transform child out of bounds 1 Answer

Error: not a member of 'UnityEngine.GameObject[]'. 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