Not using If statement to check subsribers

Instead of using If statement to check if there is something subscribed you could just use _onLevelUpAction?.Invoke();
Where ? does same work but it is cleaner and you will mostly see this in others code so it is also better to get used to it so good Idea would also be to at least mention it in course so people could recognize it when they will see it in others code.

1 Like

I use the null coalescing operator all the time. Great tip.

1 Like

I donā€™t know if this is still an issue, but there may be some problems associated with using the null coalescing operators in Unity. There is a lot of discussion out there on this topic.

1 Like

PharmacyBrain
I agree that Null Coalescing operator are evil and should be avoided as much as possible. I also only use them when calling methods when checking if there are subscribers listening. As this type of using it is very often used and known by most people there is not big problem using it. So I think it needs a shoutout in course to inform students that it exists, so they wont get confused when they will eventually come across it.
I personally didnt yet come across someone who would be using:
sr ??= GetComponent();
myList ??= new List();
And I hope I wont be seeing those for some time because It would be hard to understand sometimes meaning of them.

1 Like

argh, wrong word. Null conditional operator is what I meant.

But this conversation gives me pause to consider it.

Itā€™s still ā€œan issueā€. I was just reading about this in the Unity manual the other day.

The ? and ?? operators are can give incorrect results when for anything deriving from UnityEngine.Object and should not be used with monobehaviours or scriptable objects.

There should be a warning in your intellisense that says something similar.

1 Like

As I said, the conversation is giving me pause to consider it.

Most of the time I use it on Events and methods which seems fine, but I have to have a look at the monobehaviour side of it. I havenā€™t come across behaviour that I didnā€™t expect, so maybe Iā€™m fine or itā€™s been fixed since.

I use JetBrains Rider, and I can confirm, this construction will result in an Intellisense error:

Health health;

void hit()
{
    Health?.TakeDamage(5);
}

This will show the following warning:

ā€˜?.ā€™ on a type deriving from UnityEngine.Object bypasses the lifetime check on the underlying Unity Object

This same construction on a delegate, System.Action, UnityEvent, or event does NOT yield a warning, and these are not subject to the same inherent bug that exists from the ā€˜==ā€™ overloading Unity uses on itā€™s classes.

Itā€™s safe to use the_onLevelUpAction?.Invoke();, this will always check correctly.
It is unsafe to use MonobehaviorDerivedClass?.DoSomeMethod() for the same reasons that
foo ?? == new Foo(); is unsafe (which is too bad, as I use that construct outside of Unity all the time).

6 Likes

Note that for this issue specifically, you have to make a distinction between UnityEngine.Objects and plain C# ones that are part of the language or ones you make yourself.

ScriptableObjects, Behaviors/Components, even lower-level stuff like Mesh, any UnityEngine.Object, Unity handles its memory management. It tries its best to match a null on the C# side with the corresponding destroyed object in the C++ side but itā€™s not always correct. So a language-level null (like for null-coalescing operators) can give you unintended results. But the overloaded == operator should work as intended. ?? and ??= and ?. arenā€™t overloadable so the folks at Unity couldnā€™t fix that.

For everything else, including events, Using ? (and ?? and ??=) is fine.
You should definitely use onLevelUpAction?.Invoke() for C# events.

1 Like

This is one of the reasons that I love using Rider. When I do something that can cause an issue with Unity it gives me a nice warning.

1 Like

If you are using Visual Studio with resharper and activate the Unity plugin It is also warning you for this issue.

Yep, JetBrains really knows their stuff.

1 Like

Privacy & Terms