Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
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
Add comment · 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 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.

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

ins$$anonymous$$d 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
1

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

  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
Add comment · 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)

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

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.

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

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


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