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 gustavim15 · Sep 16, 2016 at 11:02 PM · animatorperformanceupdatevariablesperformance optimization

What is better: Set a animator variable each frame or use a if to check if need?

Hello everyone... It might be an idiot question for many of you, but I want to know what is best in performance... Use:

 void Update()
    {
        animator.SetBool("Jumped", jumping);
    }

or

 void Update()
 {
    if(animator.GetBool("jumped") != jumping)
      {
              animator.SetBool("jumped",jumping)
      }
 }


Thank you.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by jdean300 · Sep 17, 2016 at 12:27 AM

The first should be just slightly faster, but I would say there is a better way to do this. Use a property for your jumping variable, and when it's setter is called, set the animation variable if needed:

 [SerializeField] // we need this because the variable is private
 private bool _jumping
 
 public bool Jumping
 {
     get { return _jumping; }
     set {
         if (value == _jumping) return;
         _jumping = value;
         animator.SetBool("jumping", _jumping)
     }
 }

Now, you never have to call GetBool, you only call SetBool when a change occurs, and the logic for setting the animator values is hidden from the rest of your code. Just make sure you do not change the _jumping value. Do not do _jumping = false; anywhere in your code (besides in the property...) and instead use Jumping = false;

Comment
Add comment · Show 2 · 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 gustavim15 · Sep 17, 2016 at 07:57 PM 0
Share

Thank you man... Perfect explanation!

avatar image Mehrdad995 · Feb 01, 2020 at 06:25 PM 0
Share

I don't understand why implementing the code inside the boolean is more performant than implementing it normally inside an updating function. because you will as well set the boolean inside an updating function too!

avatar image
0

Answer by Reid_Taylor · Apr 08, 2020 at 12:57 AM

Mehrdad995 : I think that get { } set{ } properties are only checked when the returned variable is changed. So it sets the bool when changed instead of every frame ( I don't pretend to know what I'm talking about lol, but I think that's right ) .

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

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

76 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

Related Questions

MonoBehaviour Update() performance vs handling updates in a single UpdateManager script 0 Answers

Animator.Play() in update, Performance?? 0 Answers

Colliders vs Transform Position for performance 3 Answers

Question regarding TileMap Performance 0 Answers

Sprite shape culling performance 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