How can i call the state once inside the Update function ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Animations : MonoBehaviour
{
   public enum AnimatorStates
   {
       WALK, RUN, IDLE
   }

   private Animator _anim;

   void Awake()
   {
       _anim = GetComponent<Animator>();
   }

   public void PlayState(AnimatorStates state)
   {
       string animName = string.Empty;
       switch (state)
       {
           case AnimatorStates.WALK:
               animName = "Walk";
               break;
           case AnimatorStates.RUN:
               animName = "Run";
               break;
       }
       if (_anim == null)
           _anim = GetComponent<Animator>();

       _anim.Play(animName);
   }

   void Update()
   {
       if (MyCommands.walkbetweenwaypoints == true)
       {
           PlayState(AnimatorStates.RUN);
       }
       else
       {
           PlayState(AnimatorStates.IDLE);
       }
   }
}

The code is working fine but i don’t think it’s right to call the PlayState so many times in the Update. I want that if it’s RUN,WALK or IDLE state to call it only once in the Update function.

Once walkbetweenwaypoints is true or false it will call the state nonstop in the Update function.

Sounds right to me. So long as the animation continues, until you change its state, there would be no need to keep calling it in the Update function. Simply set the animation to play only when it’s changed, based on event/input (whatever circumstance(s) can alter your state).

The script is attached to a ThirdPersonController.
In another script i change the walkbetweenwaypoints to true or false.
So the changes of the states should be in this script the Animations.

I didn’t understand in what event or in what script should i change the State ?

whenever you change the state, you can call the PlayState() method with the new State value.
From what you just said, I would imagine that would be in “another script” (yielding true/false = run/idle). Also imagine it can change to walk somewhere, so calling PlayState there, too :slight_smile: