- Home /
What does this statement construction mean?: if (null != anim)
From the Unity documentation (https://docs.unity3d.com/ScriptReference/Animator.Play.html), the following code example is given:
public class Move : MonoBehaviour
{
private Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (null != anim)
{
// play Bounce but start at a quarter of the way though
anim.Play("Bounce", 0, 0.25f);
}
}
}
}
I understand when statements like if (x != null)
means but
What does it mean when null and the variable positions are swapped as above?
What does it mean to have your animator be null or not null? Is it just checking that an animator exists?
If so, that really a necessary step?
Yes it's a redundancy check to see if the animator is null. The question is it "necessary" would be no, but that's why they call it redundancy. It's a good thing in program$$anonymous$$g, in moderation. If you're writing code that you know for 100% certainty a member or field will NEVER be null, you can safely assume its existence without a redundancy check, however if there is even a .000001% chance it could be null, you should check for that, because otherwise it could mess up your program and you may never know why.
Do you know why null is placed before the variable name?
null != anim
ins$$anonymous$$d of
anim != null
Are these two statements exactly the same?
as far as I'm aware they are @RobAnthem, @bunny83?
Answer by zereda-games · Feb 19, 2019 at 05:50 PM
Read post above first
null means: gone, missing, or emptylist/array,
!: means Not<- , aka, opposite, isn't this.
anim is referencing an Animator class
anim = GetComponent(); Literally was just reading and corrected on the post i was on previous of yours. GameObject.FindWithTag("StringValue"); seems to be most popular, FindObjectOfType""(); works but is very expensive on performance to do often as it looks through the Whole scene for the first object of that type that it can find. were FindWithTag, only looks at objects with tags and only looks for a specific string if it is in the Tag's List..
So do you know if there's a difference in the if statement between saying:
null != anim
or
anim != null
Are these two interchangeable?
Those are interchangeable and any sane person would write
if (anim != null)
$$anonymous$$ore specifically the un-equal operator !=
simply compares two values for inequality and returns a boolean (true or false).
Note that !=
is one operator and has nothing to do with the unary !
not operator. Though it serves a similar purpose.
Your answer
![](https://koobas.hobune.stream/wayback/20220612192027im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
On Hit play animation 0 Answers
Animate an Object by letting it play out in when I press play? 0 Answers
Animator.Play()? 1 Answer
How do i stop an animation automatically in Unity 4.6 1 Answer
2D Animation does not start 1 Answer