How to call animator.SetBool() using UnityEvent?

Hello, Team!

Can anyone help me with this?

I want to use UnityEvent system to call animator.SetBool() method from other object, but there is no such method in popup.

Is there a way to make it appear in dropdown selector inside inspector?

Cheers,
Alex

If you want to set the arguments in the event inspector, the issue is that Animator.SetBool requires two arguments passed, but the UnityEvent inspector only supports setting up a single argument.

As far as I know the easiest way to deal with this is to use a wrapper script e.g.:

public class AnimatorUnityEventHandler
{
    public Animator animator;
    public string boolName = "myBool";

    public void SetBool(bool value)
    {
        animator.SetBool(boolName, value);
    }
}

Here you can call the SetBool method with a single argument via UnityEvent as long as you set up this component with the bool parameter name in the inspector.

Other than that you can create variations of the same idea with UnityEvent e.g. a custom UnityEvent type that receives a the custom parameter data.

1 Like

You can create an event and assign the SetBool method as a dynamic event in the inspector.

[System.Serializable]
public class AnimationBoolEvent : UnityEvent<string, bool> { }

public AnimationBoolEvent MyEvent;

What you can do is

void Crouch(bool flag){
    animator.SetBool("Crouch", flag);
}

It’s a little old this post but I arrived here with the same problem today, so I expose my solution. I made an AnimatorExtension monobehaviour like this:

    [RequireComponent(typeof(Animator))]
    public class AnimatorExtension : MonoBehaviour
    {
        Animator animator_;
        // Start is called before the first frame update
        void Awake()
        {
            animator_ = GetComponent<Animator>();
        }

        public void SetBoolTrue(string name) => animator_.SetBool(name, true);

        public void SetBoolFalse(string name) => animator_.SetBool(name, false);

    }

It’s similar to cmyd solution, but I think is more generic have two calls for true/false and make variable the name argument.