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 /
avatar image
0
Question by Cbjfan1 · Dec 16, 2013 at 01:32 AM · errorgameobjectdestroynullmissing

missing gameobject ≠ Null?

I made a script not too long ago, and it's supposed to detect if the variable 'target' is empty. When the object is destroyed, however, it says missing gameobject, and null no longer works. Can anyone tell me how to detect a missing gameobject? Here is the script: (asterisks where this part of the script is)

 var ifenemy : boolean;
 
 var Health : int;
 
 var Deadreplace : Transform;
 
 
 var target : Transform; //the enemy's target
 
 var tarpos : Vector3;
 
 var moveSpeed = 50; //move speed
 
 var rotationSpeed = 3; //speed of turning
 
 var thispos : Vector3;
 
 var inrange = false;
  
 var myTransform : Transform; //current transform data of this enemy
  
 function Awake()
 {
      myTransform = transform; //cache transform data for easy access/preformance
 }
  
 function Start()
 { 
      Health = 100;
  
 }
  
 **function Update () {
 if(ifenemy == false){
 target = GameObject.FindWithTag("Enemy").transform; //target the player
 }
 if(ifenemy == true){
 target = GameObject.FindWithTag("GoodGuy").transform; //target the player
 }
 if(target != null){
 print("working");
     inrange = false;
     shootobj.GetComponent(AAShoot).canshoot = false;
     }**
 tarpos = target.position;
 thispos = transform.position;
  
  
 if(Vector3.Distance (thispos, tarpos) < 175){
             inrange = true;
             shootobj.GetComponent(AAShoot).canshoot = true;
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
                 //move towards the player
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     }
     if(Vector3.Distance (thispos, tarpos) > 175){
     inrange = false;
     shootobj.GetComponent(AAShoot).canshoot = false;
     }
     }
 
 function OnCollisionEnter (collision:Collision)
 {
     if (collision.gameObject.tag == "Damage")
     {
     Health -= 20;
     }
     
    }
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
1

Answer by iwaldrop · Dec 16, 2013 at 02:36 AM

You continue to reference target outside of the null check. Simply paying attention to which line the error occurs at (or double clicking the error in the console) will let you know exactly where the problem is.

At line 40 you check if the target is not null, but at lines 45 and 53 you reference it outside of the null check.

What you might consider doing is the following on line 40/41:

 if (target == null)
     return;

Then do everything that you would do if the target was not null. Also, properly format your code. It helps a lot when debugging things, plus it reflects how much you care about your own work.

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 richardzzzarnold · Mar 02, 2015 at 11:29 AM 0
Share

Hi did you ever work out how to work around this? I have a very similar problem and can't work it out for the life of me

avatar image Ranth · Jun 02, 2015 at 10:59 PM 0
Share

I am in the same boat. Any ideas?

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

17 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Why am i getting a null reference? 1 Answer

Can't copy Gameobject into array, only refer to an existing one. 1 Answer

Destroyed gameobject become Null ? 1 Answer

When Is This Script Destroyed? 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