• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Siggytron · Feb 19, 2019 at 05:29 PM · animatorplaynull

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

  1. What does it mean when null and the variable positions are swapped as above?

  2. What does it mean to have your animator be null or not null? Is it just checking that an animator exists?

  3. If so, that really a necessary step?

Comment

People who like this

0 Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RobAnthem · Feb 19, 2019 at 05:38 PM 2
Share

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 programming, 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.

avatar image Siggytron RobAnthem · Feb 19, 2019 at 06:15 PM 0
Share

Do you know why null is placed before the variable name?

null != anim

instead of

 anim != null

Are these two statements exactly the same?

avatar image zereda-games Siggytron · Feb 19, 2019 at 06:18 PM 1
Share

as far as I'm aware they are @RobAnthem, @bunny83?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by zereda-games · Feb 19, 2019 at 05:50 PM

Read post above first

  1. null means: gone, missing, or emptylist/array,

  2. !: means Not<- , aka, opposite, isn't t$$anonymous$$s.

  3. anim is referencing an Animator class

  4. 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..

Comment
Siggytron

People who like this

1 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Siggytron · Feb 19, 2019 at 06:18 PM 0
Share

So do you know if there's a difference in the if statement between saying:

null != anim

or

anim != null

Are these two interchangeable?

avatar image Bunny83 Siggytron · Feb 19, 2019 at 06:28 PM 2
Share

Those are interchangeable and any sane person would write

 if (anim != null)

More 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.

avatar image zereda-games Bunny83 · Feb 19, 2019 at 06:33 PM 0
Share

Thanks, i hate being wrong.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

121 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Animation not playing? 1 Answer

How do I play my animations w. Mechanim? 1 Answer

Problem with Animator.Play() 0 Answers

Animation not be found 1 Answer

Animator problem start animation 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges