Draken Forge37
Draken Forge37

Reputation: 61

Unity 3D Trying to reference script that is attached to empty state in the animator. GetComponent Not Working

Is there a way to reference the script that is attached to the empty state in the animator? I have a script called ResetAnimatorBool and I want to reference it from another script but GetComponent<ResetAnimatorBool> does not work

The idea is to see if the bool is false(if the animation is finished and back to the empty state) and if it isn't and the animation is still playing, then disable controls until the animation is finished.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
    
public class ResetAnimatorBool : StateMachineBehaviour
{
    [Header("Disable Root Motion")]
    public string disableRootMotion = "disableRootMotion";
    public bool disableRootMotionStatus = false;
    
    [Header("Is Performing Action Bool")]
    public string isPerformingAction = "isPerformingAction";
    public bool isPerformingActionStatus = false;
    
    [Header("Is Preforming Quick Turn")]
    public string isPerformingQuickTurn = "isPerformingQuickTurn";
    public bool isPerformingQuickTurnStatus = false;
    
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        animator.SetBool(disableRootMotion, disableRootMotionStatus);
        animator.SetBool(isPerformingAction, isPerformingActionStatus);
        animator.SetBool(isPerformingQuickTurn, isPerformingQuickTurnStatus);
    }
}

Upvotes: 1

Views: 247

Answers (1)

KiynL
KiynL

Reputation: 4266

First refer to the animator you want and then use Animator.GetBehaviour<>():

public Animator playerAnimator;
public void Start()
{
    playerAnimator = GetComponent<Animator>();

    var disableRootMotion = playerAnimator.GetBehaviour<ResetAnimatorBool>().disableRootMotion;
    
    Debug.Log(disableRootMotion); // disableRootMotion
}

Also do the following to get all the behaviors.

var behaviours = playerAnimator.GetBehaviours<StateMachineBehaviour>();

Upvotes: 0

Related Questions