Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Noah-1 · Mar 09, 2011 at 05:54 PM · objectnull

How to check if something is null?

Hey guys I have a script that creates joysticks, the problem is that when I load anoter level, this error appears:

MissingReferenceException: The object of type 'GUITexture' 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.

How can I make sure to make it null?

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

3 Replies

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

Answer by yoyo · Mar 09, 2011 at 06:20 PM

In C# you need to compare to null ...

if (myTexture != null)
   GUI.DrawTexture(Rect(0,0,500,500),myTexture);
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 AliAzin · Mar 11, 2011 at 07:39 AM 1
Share

You don't need to do that nether in c# nor Java . "if (myTexture)" is equal to "if (myTexture != null)" in both languages .

avatar image yoyo · Mar 11, 2011 at 03:57 PM 1
Share

I didn't believe you so I went and tried for myself -- you're right, thanks Ali! Turns out UnityEngine.Object has an implicit operator for conversion to boolean, which makes this work. Normally in C# you have to explicitly compare with null -- for example you can't say "if (myString)" (assu$$anonymous$$g myString is a string), you need to be explicit and say "if (myString == null)". I'd suggest using comparison with null even though its not necessary, otherwise you will be confused when it doesn't work for a non-Unity object.

avatar image
11

Answer by AliAzin · Mar 09, 2011 at 06:10 PM

you can simply check it like this:

if (myTexture)
   GUI.DrawTexture(Rect(0,0,500,500),myTexture);
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 Nick62 · Jul 17, 2018 at 12:02 PM 0
Share

Where could i read about this case of if statement: if (myTexture)

I want to be shure, that it is equivalent of if (myTexture != null)

Thank you!

avatar image Hellium Nick62 · Jul 17, 2018 at 12:16 PM 2
Share

This is the same, indeed. Here is the code of the UnityEngine.Object class every objects derive from :

     // Does the object exist?
     // Called when checking `if (myTexture)`
     public static implicit operator bool(Object exists)
     {
         return !CompareBaseObjects(exists, null);
     }

     // Called when checking if( myTexture != null )
     public static bool operator!=(Object x, Object y) { return !CompareBaseObjects(x, y); }
avatar image
1

Answer by Matheuz · Apr 07, 2020 at 07:30 PM

If you want to check if the underlying object has been destroyed:

 if (_yourTexture != null)

If you want to check for actual null reference:

 Object.ReferenceEquals (_myTexture, null)

Keep in mind that your example threw a MissingReferenceException and not a NullPointerException. These might look like the same, but they mean different things. On NullPointerExceptions, the variable actually points to null. On MissingReferenceException, the underlying object might have been destroyed, but the script might still leave. This article explains the difference between them.

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

2 People are following this question.

avatar image avatar image

Related Questions

NullReferenceException: Object reference not set to an instance of an object 1 Answer

Change 3D Object by pressing Gui Button 1 Answer

Find object that enters trigger 1 Answer

How do I check a variable of an instantiated object from another instantiated object? 1 Answer

[SOLVED]Object selection scrip doesn't work. 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