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 ewoutee · Apr 22, 2021 at 11:44 AM · null

Is there a difference between these null checks?

After reading this unity article about unity specific null checks I was wondering about shorthand null checks for unity objects. I never found a definitive answer on the forums or documentation so I decided to ask here. Is there a difference between:

 if(_myObject)

and

 if(_myObject != null)

and

 if(!_myObject == null)

I know the last one doesn't make a lot of sense but I decided to add it for completeness sake. Will these ways of checking if a unity object is null always return the same values? Is there a difference in performance? Or are they exactly the same under the hood?

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
2
Best Answer

Answer by Hellium · Apr 22, 2021 at 01:07 PM

Here is the actual implementation of UnityEngine.Object

     public static bool operator==(Object x, Object y) { return CompareBaseObjects(x, y); }

     public static bool operator!=(Object x, Object y) { return !CompareBaseObjects(x, y); }

     // Does the object exist?
     public static implicit operator bool(Object exists)
     {
         return !CompareBaseObjects(exists, null);
     }
     static bool CompareBaseObjects(UnityEngine.Object lhs, UnityEngine.Object rhs)
     {
         bool lhsNull = ((object)lhs) == null;
         bool rhsNull = ((object)rhs) == null;

         if (rhsNull && lhsNull) return true;

         if (rhsNull) return !IsNativeObjectAlive(lhs);
         if (lhsNull) return !IsNativeObjectAlive(rhs);

         return lhs.m_InstanceID == rhs.m_InstanceID;
     }



As you can see:

 • if(_myObject) will call implicit operator bool which basically checks whether _myObject is null using CompareBaseObjects


 • if(_myObject != null) will call bool operator!= which calls CompareBaseObjects too


However, your last condition will "throw" a cs0472 warning since !_myObject returns a bool, and comparing a bool against null does not make sense.

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 ewoutee · Apr 22, 2021 at 01:10 PM 0
Share

So the third check would have to be if(!(_myObject == null)) for it to make sense?

avatar image Hellium ewoutee · Apr 22, 2021 at 01:17 PM 2
Share

Indeed. And it would be very similar to the 2nd example. You just invert the result returned by the overload of the == operator.

avatar image Bunny83 ewoutee · Apr 22, 2021 at 02:02 PM 0
Share

Note that Unity has a reference github repository with the source code of the UnityEngine dll. The bool type conversion operator and the two comparison operators can be found there.


Please keep in $$anonymous$$d that this repository is not open source. It's just there for reference. Make sure you read the license before extracting or using any code from there.

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

116 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how can I check if an object is null? 4 Answers

what is null 1 Answer

XMLDocument GetElementByID returning null 1 Answer

Confused as to why I'm getting a NullReference Exception 1 Answer

Destroying an object not working 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