cant's change animation state of animator from script unity code example

Example 1: how to change animator controller in script unity

//STEP1: Inside the Assets folder, create a new folder called: Resources 
//STEP2: Now Create an Animation folder Inside the Resources folder
//STEP3: Then put the Animator Controller inside this folder (for this examle we'll call the controller Bob)
//STEP4: Now add this to the gamObject that holds the Animator componentAnimator animator;

void Start()
{
	animator = gameObject.GetComponent();
}

void Update()
{
    //I put it inside this If Statement to avoid Errors/Warnings but its not 100% necessary
    if(animator.gameObject.activeSelf)
    {
		animator.runtimeAnimatorController = Resources.Load("Animation/Bob") as RuntimeAnimatorController;
        //OR
        animator.runtimeAnimatorController = Resources.Load("Animation/Bob");
    }
}

Example 2: how to set an animator parameter through the animator unity

//click the add behaviour button on a state in the animator controller
 	//and add a script. Just put the name of the bool in the inspector
 public class AnimatorResetBoolAtEnd : StateMachineBehaviour {
 
         [SerializeField]
         private string booleanVariableName;
 
   
         // OnStateExit is called when a transition ends and the state 
         //machine finishes evaluating this state
         override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
         {
             animator.SetBool(booleanVariableName, false);
         }
 
        
     }

Tags:

Misc Example