Search Issue Tracker

By Design

Votes

8

Found in

2017.1.0f3

Issue ID

941880

Regression

No

C# 6 null conditional access operator (?.) throws MissingComponentException instead of recognizing as null

Scripting

-

To reproduce:
1. Open attached Unity project "CSharpBug.zip"
2. Open "TestScene" scene
3. Inspect both "ThrowsException" and "Safe" GameObjects and scripts attached to them
4. Go into play mode

Expected result: Both scripts should yield the same results (print the success message)
Actual result: In Throws Exception script, there is a ?. operator which works as a check whether the object is null or not. Instead of ignoring the whole line of script, as meshfilter in this case is null, MissingComponentException is thrown.

Reproduced on: 2017.1.0p3, 2017.2.0b7, 2017.3.0a4

  1. Resolution Note:

    UnityEngine.Object and those who inherit it, do not support null conditional operator.
    The Docs are updated to reflect this

Comments (8)

  1. chatrat12

    Apr 29, 2020 17:50

    I have 260 null-conditionals in our project :O Most of them are still valid however.

  2. chatrat12

    Apr 29, 2020 17:37

    I'm sure fixing this is no easy feat, but this really bums me out. I've been using the null-conditional but didn't know it wasn't working since I was never testing anything that was let's call 'nullish'. Not supporting ?. and ?? is confusing since its part of the C# standard.

    Side note: Why doesn't the Unity team have a way to get metrics on the features we want anymore? The feedback service is gone and we can't vote on issues that are not active. I'm sure this would have a lot more votes if we were allowed to vote on it.

  3. JPhilipp

    Feb 14, 2020 15:11

    This is still happening Unity 2019.3.0f6 (latest stable). I'm trying to shorten

    if (pickSound != null) { pickSound.Play(); }

    to

    pickSound?.Play();

    (where pickSound is an optional SerializeField AudioSource) and the latter version won't work.

  4. sumner

    Apr 24, 2019 18:46

    Please change... we like C# standards

  5. Krstn

    Mar 19, 2019 11:21

    There is an inspection in JetBrains Rider and ReSharper which detects this and warn about it. It's a good idea to change the severity to Error.

  6. better_walk_away

    Nov 13, 2018 00:19

    Note that delegate function works, let's say we have a delegate "DoSomething", "DoSomething.?.Invoke()" works as expected.

  7. OndrejP

    Oct 24, 2018 07:39

    This is caused by Unity's stupid decision to overload 'operator==' and use of fake null objects.

    Because null coalescing operator and conditional member access does not call overloaded operator==, but does identity comparison instead, it simply does not work. (fake null object is not identical to null)

    This is major pain in the ass for people using C# and learning Unity. You simply "check for null" by standard language construct and it backfires horribly.

    Unity, for gods sake, just make a breaking change a remove this crap, sooner is better.
    Together with implicit cast of Object to bool. This is one of few things which were common for C++, but shouldn't be done in C#.

    Or at least add some option to Unity to never use FAKE NULL objects and always use classic null instead.

  8. cedavis

    Jul 02, 2018 20:22

    Issue still occurs in Unity 2018.1.6, except I'm getting a Null Reference Exception with the code ```sprite?.texture == null```

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.