Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by cocopuffs · Jun 17, 2011 at 06:36 AM · transformnullmissingreferenceexception

how can I check if an object is null?

in myconsole i keep geting this error when a dragon(with the name of cube) gets destroyed()

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.

i was reading this post and from what i understand i would change it to this?

if (Transform) transform(Rect(0,0,500,500),transform);

is this wrong ...or what is the proper way to do this?

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 Rigume.du · Jun 17, 2011 at 06:11 PM 0
Share

having the same problem over here var enemies: GameObject;

function OnDestroy () { enemies.SetActiveRecursively(true);

 if(gameObject==null){
 Destroy(this);
 }
 else;

}

avatar image Eric5h5 Rigume.du · Jun 17, 2011 at 06:28 PM 0
Share

Please don't post comments as answers.

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Eric5h5 · Jun 17, 2011 at 06:45 AM

Yes, that's wrong; the correct way is to refer to the object, not the type.

 var someObject : Transform;

 function Start () {
     if (someObject != null) {
         // do something
     }
 }
Comment
Add comment · Show 3 · 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 cocopuffs · Jun 17, 2011 at 06:48 AM 0
Share

ohhhhh ok i see when its asking about transform its really talking about the object that got deleted .....thank you so much8)

avatar image cocopuffs · Jun 17, 2011 at 06:53 AM 0
Share

i added this script to the dragon but it still shows the same message...y?

avatar image Eric5h5 · Jun 17, 2011 at 06:29 PM 0
Share

What I wrote is an example. You have to write correct code for your own circumstances.

avatar image
3

Answer by FriedrichWessel_Inno · May 17, 2018 at 05:27 PM

For future reference:

Unity overloads the == operator. Thats why if(gameObject == null) will return false even though the object is destroyed ( but not collected by the GC )

The only safe way to do this check is avoid the == operator and use the ?? instead - that one is not overwritten and plain C#

 if(gameObjectToTest??false){
     // this will only execute if gameObject is not destroyed
 }

Detailed information can be found here : https://blogs.unity3d.com/2014/05/16/custom-operator-should-we-keep-it/

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 Matheuz · Apr 07, 2020 at 07:45 PM 2
Share

I believe the == operator does exactly the opposite of what you've described. Unity's implementation of the == and != in the UnityEngine.Object class checks if the underlying engine components (transform, gameObject) were destroyed. In that case, a check like if (gameObject == null) Should return null for an object that has been destroyed. Notice that the exception that was thrown was a $$anonymous$$issingReferenceException and not a NullPointerException. The object isn't null, but its reference to the engine component (on the engine's native side) gos destroyed.

You can read a bit more about this here and here.

avatar image
0

Answer by atulpateltmspl · Mar 21, 2020 at 09:41 AM

   public ItemContent itemcontent;  
    // Object Declartion and make a class
   // ItemContent Just Check object is null or not..simply
   if(itemcontent != null) {
    Debug.Log("item content is notnull");
    } else { 
      Debug.Log("item content is null"); }
  // -----------------------OR-------------------------------- 
   if( itemcontent == null) 
   {
    Debug.Log("item content is null"); 
     } else {
    Debug.Log("item content is not 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
avatar image
0

Answer by Thunderkilll · Sep 10, 2020 at 01:18 PM

I think you have to use DontDestroyOnLoad(gameObject); in this way your game object will not get destroyed . Use this when the script loads ( void start()) I hope this is usefull in any way

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.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

MissingReferenceException Help 1 Answer

script trying to access a null gameobject's collider... can't fix 2 Answers

Best practice handling deleted object refences during SceneManager.LoadScene 0 Answers

Expand script to stop camera from following player 2 Answers

How Do Camera Follow Mullti Playeres 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