Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
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
Add comment · 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

· Add your reply
  • Sort: 
avatar image
1
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
Add comment · 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
1

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

Have you tried changing

 Destroy(this.gameObject);

to

 Destroy(gameObject);
Comment
Add comment · 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 ;)

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.

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

Error CS0029: Cannot implicitly convert type to UnityEngine.UI.Transform 1 Answer

BCE0005: Unknown identifier: 'transform' 2 Answers

transform.position.x brings up error 1 Answer

Return Enemy To Start Position 1 Answer

Error in localPosition 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