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

Scripting

-

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)

Comments (6)

  1. 6fa893f03d1bb83c17c06168a2cfe849?d=mm

    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;

  2. D78f33538a927d68bdc6b85d38db6a3e?d=mm

    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"

  3. 970e9c45ed9000ff6e14c584c1696c1b?d=mm

    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.

  4. 67a125e53965d6dd83277c0ed48d2a37?d=mm

    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.

  5. 790140d62c5523c213edec83998d8a4c?d=mm

    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

  6. F554d5d7c36304d4d53bbb72d8927c97?d=mm

    AmazingRuss

    Nov 02, 2016 21:53

    This is not fixed.

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.