Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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
0
Question by tategarringer · Jan 01, 2021 at 08:16 AM · objectnull reference exception

Difference in null behaviour between public/[SerializeField]private and private UnityEngine.Object

Hey all, relatively new to Unity so I apologize if this is common knowledge, but I was unable to find anything on this specific topic.


I was attempting to do an inline null check for a public Rigidbody2D component before utilizing its velocity on another component, the simplified version looks essentially like this:


 move += rigidBody2D is null ? new Vector2(0, player.velocity.y) : new Vector2(rigidBody2D.velocity.x, 0);


Strangely, I found when running tests for this, if rigidBody2D wasn't assigned a value, it was throwing an error instead of evaluating true at the null check and continuing appropriately. I did some poking around and ultimately disabled everything but a single GameObject in my project and threw this short test on it. It's worth noting that throughout this process, I purposely never assigned anything to the public references (or the [SerializeField]private references further down).


 using UnityEngine;
 
 public class UnityObjectPublicPrivateNullCheck : MonoBehaviour
 {
     private UnityEngine.Object privateUnityObjectNullCheck;
     public UnityEngine.Object publicUnityObjectNullCheck;
     private object privateSystemObjectNullCheck;
     public object publicSystemObjectNullCheck;
 
     void Start()
     {
         //null check on Unity object type
         Debug.Log(privateUnityObjectNullCheck ?? true); //returns true, expected
         Debug.Log(publicUnityObjectNullCheck ?? true); //returns false, huh???
 
         //null check on System object type
         Debug.Log(privateSystemObjectNullCheck ?? true); //returns true, expected
         Debug.Log(publicSystemObjectNullCheck ?? true); //returns true, expected
     }
 }


This effectively gets my point across, but I did some further testing and found that public UnityEngine.Object are not actually returning null, but "null", whereas private System.Object, public System.Object, and private UnityEngine.Object all return null, expectedly.


One more additional test, I wagered a guess that this might have something to do with Unity's serialization for the inspector, so I went ahead and put this line in the script:


 [SerializeField]private UnityEngine.Object serializedPrivateUnityObjectNullCheck;
 ...
 //null check on Serialized private Unity object type
 Debug.Log(serializedPrivateUnityObjectNullCheck ?? true); //returns false, huh???


Finding the culprit, I'm still a bit confused. Surely, this is this intended behaviour, but why? Am I destined to have to check for UnityEngine.Object.ToString() == "null" for public and [SerializeField] private or is there a more efficient workaround?


Final note: I am running Unity 2020.1.17f1, not sure that matters.

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

2 Replies

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

Answer by tategarringer · Jan 01, 2021 at 09:44 AM

I was directed to a duplicate of this question on StackOverflow for anyone curious about this. Turns out, Unity has an overriden implementation of Equals and null, this link covers it and *why* in more depth. The solution is simply dropping the null comparison in favor of a boolean comparison when dealing with UnityEngine.Object.

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 sacredgeometry · Jan 01, 2021 at 08:52 AM

Shouldnt move += rigidBody2D is null ? new Vector2(0, player.velocity.y) : new Vector2(rigidBody2D.velocity.x, 0);

be

move += rigidBody2D == null ? new Vector2(0, player.velocity.y) : new Vector2(rigidBody2D.velocity.x, 0);


Also no there isn't anything in the access modifier that would change the way null coalescing works what there is is that unitys editor adds a caching layer on-top of public fields. i.e. check what the value is in the inspector

Comment
Add comment · Show 5 · 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 tategarringer · Jan 01, 2021 at 09:00 AM 0
Share

I would argue that that's down to a stylistic preference because those both say the same thing, but that's not the question at hand anyways.

avatar image sacredgeometry tategarringer · Jan 01, 2021 at 09:17 AM 0
Share

You are right I didnt realise they added null checking to is pattern matching.

p.s. I answered the question in the second part of the answer

avatar image tategarringer · Jan 01, 2021 at 09:16 AM 0
Share

Thanks for taking time to provide some clarity. Addressing the second part that i missed initially (sorry), this is still a bit confusing to me. I can understand the fact that Unity has implemented a custom method for returning null fields exposed to the editor, but I guess I'm curious as to why this would be preferable to just returning a legitimate null value? What's the workaround to checking UnityEngine.Object type for null values? Is there an already existing method for this that I'm unaware of that covers both System.Object null and UnityEngine.Object "null"?

avatar image sacredgeometry tategarringer · Jan 01, 2021 at 09:46 AM 1
Share

Oh sorry I think I get what you mean now.

Unity overloads the equality operator (edit: sorry I havent slept probably shouldnt be trying to code)


https://blogs.unity3d.com/2014/05/16/custom-operator-should-we-keep-it/

https://nosuchstudio.medium.com/why-are-null-coalescing-operators-evil-in-unity-16f5a88d6071#:~:text=So%20Unity%20Engineers%20decided%20to,actual%20value%20is%20not%20null.&text=operators%20for%20Unity%20objects.


It has its reasons but it may explain what you are seeing.

avatar image tategarringer sacredgeometry · Jan 01, 2021 at 09:49 AM 1
Share

I was just redirected to a link on StackOverflow about this exact thing only moments before you posted this, so I went ahead and directed the answer to that. Thank you for taking time to post these links and help.

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

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

Related Questions

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

Unparenting and then deleting a child part of a prefab, SOMETIMES child throws null reference exception? 1 Answer

NullReferenceException: Object reference not set to an instance of an object 2 Answers

destroy object on collision 1 Answer

drag object ios 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