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.

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;

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