I wish this code would work, it would be so useful!
public GameObject player;
player?.transform.position = Vector3.zero; // Auto null-check
I wish this code would work, it would be so useful!
public GameObject player;
player?.transform.position = Vector3.zero; // Auto null-check
me tooâŚ
Everything about how Unity deals with ânullâ drives me nuts.
Just tonight I was wrestling with that annoying thing where unity assigns an actual object to any UnityEngine.Object field that is serialized if itâs null when serialized. The object is a âdeadâ object that evaluates true if you compare it to ânullâ, but thereâs an actual C# object sitting there. And since the â== nullâ operator doesnât work if you type it to System.Object, you can get false positives.
Grrrrrrrrrrr.
Like why Unity, why do you have to stick an object in if itâs null? Leave it null!
I was about to critique you for misusing the ternary operator before I did a quick Google check. You taught me something new!
Unfortunately the ?. is not overloadable, otherwise I expect Unity would have handled it along the lines of ==.
See the article in my second link. They have some really good reasons for doing what they do.
Edit: To clarify, they were good reasons back when the decisions were made. With current versions of C# Iâm not sure that the same decisions would be ideal.
Their reason is this:
And I donât agree thatâs a good reason.
It is certainly a reason, but âgoodâ is subjective.
Iâm of the opinion that early on they made a bunch of choices based on making ânoviceâ lives better, well punishing professional level developers. But since their target audience was novices at first, it worked for them. Now as their user base has matured, theyâre taking the hit for that.
Mind you, when I said:
I was being rhetorical. I am aware of the article you posted. I just think all those choices they made back then are terrible choices. Theyâre up there with how Mesh.vertices, Renderer.material, etc, returns clones/dupes/added behaviour. Nothing about the property naming conveys that itâs creating a clone and because itâs a property you feel as if you should be able to directly manipulate it, but actually you canât (depending the property). Better named methods like âCopyVerticesâ and âSetVerticesâ would be more descriptive. This is a much longer rant Iâve had elsewhere on these forums though, so Iâm going to leave it there.
In the same sense⌠I understand why they still exist. And we have to deal with our bad choices down the line. Changing the API now breaks existing code bases⌠so doing so has to be deliberate, slow, and with the communities understanding. So they donât break code bases.
But pretending they were âgoodâ choices doesnât help at all.
It was a good choice back then. It wouldnât be a good choice today.
BTW, on that note, I really donât like this notion that Unity does not take up the decision and introduce breaking changes on major version change. After the LTS version itâs a good place to introduce this kind of things.
The user base always will demand everything and double that. Asset store creators also always will ask for zero change to not to create extra work for them.
But staying on old decisions fearing to break old code is bad. If the old code is important, one should stay on the last LTS, problem solved.
Also if AS Publishers want to support new Unity version, then they should work on it, otherwise they support last LTS version.
So if I can piggy-bank on this, I would add to the wish-list, that the old debts should be paid at major version changes.
Hah, funnily enough I was trying the same thing yesterday.
Yeah I agree, this would be a nice addition.
The ? operator and the bogus null objects are seperate issues.
The ? operator doesnât work because you canât overload ?, while you can overload ==. I they changed the == overload, checking for destroyed objects would be much less convenient, so itâs for sure not a free trade off.
Thereâs two real choices - either (x == null) also checks if x is destroyed, or (x?.a) works for UnityEngine.Object types. I prefer x == null working as it does.
The bogus null objects only exist in the editor, and are there in order to give you MissingReferenceExceptions with helpful error messages (âThereâs no X component attachedâ) instead of NullReferenceExceptions. Those donât exist in builds, and are the reason why every GetComponent call in the editor that doesnât find the component causes 0.6 kb of allocation.
This is insanely bad, especially due to giving different results in the editor and at runtime. This should be purged with fire.