Search Issue Tracker
Fixed
Votes
1
Found in
4.3.0b2
Issue ID
563858
Regression
No
Null coalescing operator does not work for the Transform component
Steps to reproduce :
- import the project attached
- open the scene
- press play and notice the message in the console :
Should not be null: null
UnityEngine.Debug:Log(Object)
NullCoalescingOperatorTest:Awake() (at Assets/NullCoalescingOperatorTest.cs:12)
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- ArgumentOutOfRangeException when manipulating the Project Settings search bar
- [Linux] Touch freezes in Player when any mouse button is clicked
- UnityEngine.Rendering.Volume.OnEnable() is called twice before OnDisable() creating multiple copies of the same volume when a volume is enabled right after Domain Reload when entering Play Mode
- Hinge Joint transforms are reset when toggling the "useMotor" property
- Motion Vector texture reports incorrect Mesh data when Mesh is coming from off-screen
k76
Oct 13, 2019 06:11
Still not fixed as of Unity 2019.1.1f1. For example, if my Renderer component (m_Renderer) has been destroyed, the following code will not work as it should:
return m_Renderer?.enabled ?? false; // throws a MissingReferenceException
Instead I have to do it like this:
if (m_Renderer != null)
{
return m_Renderer.enabled;
}
return false;
Vapid-Linus
Feb 27, 2018 10:51
They wrote about this issue in blog post from 2014:
https://blogs.unity3d.com/2014/05/16/custom-operator-should-we-keep-it/
Ctrl-F "?? operator"
DouglasPotesta
Aug 31, 2017 15:31
This is not fixed.
mMeshRend = mMeshRend ?? GetComponent<MeshRenderer>();
mMeshFilter = mMeshFilter ?? GetComponent<MeshFilter>();
mMesh = mMesh ?? mMeshFilter.sharedMesh;
mVerts = mMesh.vertices;
Null coalescing is only useful for cleaning up code, so it is not imperative, but it is nice to have. It should be a fairly quick to fix too.
OndrejP
Jul 29, 2017 10:23
The problem:
A) a != null ? a : b
B) a ?? b
A) Uses Object.operator==(), which is overridden for Object
B) Uses object.ReferenceEquals()
The reason why it's different is because overridden operator considers "empty" objects and "null" equals. I guess Unity uses "empty" objects for some reason instead of plain null value.
It's very annoying and hidden caveat of Unity, I wonder how they fixed it.
scsc
Jan 02, 2017 23:55
UnassignedReferenceException when a Transform is not assigned to a script and I try to use a ?? b instead of a != null ? a : b, even though they should be equivalent
AmazingRuss
Nov 02, 2016 21:53
This is not fixed.