When I did the challenge before watching how it was done in the video, I put &! hasPackage, instead of && !hasPackage, yet I got the same result. Do both work pretty much the same?
No, they don’t work the same. And you should be very careful. A single operator is an assignment, while double operators are conditional. In this case it was fine but it is very easy to break something by accidentally using an assignment operator when a conditional was intended. Fortunately, modern programming languages (like C#) have gotten wise and may show an error.
Because of this you may find older C/C++ programmers doing
if (null == obj) { } // having null first
If they swapped it around and accidentally made a typo
if (obj = null) {}
things would be rather bad for them. This would return false (I think, can’t remember) but the obj is now null because they accidentally assigned null to it. Making it a habit to swap it around helped prevent this, because it is not possible to assign obj to null and the compiler would’ve complained.
This doesn’t happen often in C#, but I’m sure there are scenarios where this happens that I can’t think of right now.
Perhaps something like
var package = GetPackage();
var hasPackage = package = null;
if (hasPackage)
{
}
here, hasPackage
will always return false (if package
is a Unity Object) because we have set both hasPackage
and package
to null.
I figured they were different, thank you for the explanation.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.