Animation wont play

Hey guys I am facing a current problem, this is part of my sprinting script, I want so when I press the shift key the animation plays. The shift key part works fine, its just the animation, this code below is the Update method:

//All the if statements below will make it so if user press any of the keys "w" "shift" with any combination the sprinting method will start
if (Input.GetKeyDown(KeyCode.W))
{
SprintButtonsPushed++;
if (SprintButtonsPushed == 2)
{
StartCoroutine(makePlayerSprint());
}
}

if (Input.GetKeyUp(KeyCode.W))
{
SprintButtonsPushed--;
}

if (Input.GetKeyDown(KeyCode.LeftShift))
{
SprintButtonsPushed++;
if (SprintButtonsPushed == 2)
{
StartCoroutine(makePlayerSprint());
}
}

if (Input.GetKeyUp(KeyCode.LeftShift))
{
SprintButtonsPushed--;
}

And this code below is the method

//this method after a few seconds will make the player stop sprinting
public IEnumerator makePlayerSprint()
{
FPSC.GetComponentInChildren<Animation>().Play("SprintAnim");

//wait 10 seconds
yield return new WaitForSeconds(10);

if(!playerPuffing.isPlaying && playOnce == false)
{
playerPuffing.Play();

playOnce = true;
}

playOnce = false;
}

But when I press shift the animation says “Default clip could not be found in attached animations list.”

If I put the animation on legacy mode it doesn’t show this message but the animation still doesn’t work.

I’m sure you’ve looked into all of this already, since you mentioned the legacy flag, but make sure you created the animation by using the Animation view while highlighting the appropriate object, etc.

Honestly, I’d recommend just adding a reference to the animation clip in your script and setting it in the inspector. Set the default clip using Animation.clip and then just use Animation.Play() without a name to play the default:

AnimationClip sprintAnim;  //Set in the inspector

...

Animation animation = FPSC.GetComponentInChildren<Animation>();
animation.clip = sprintAnim;
animation.Play();

I did do that. I pressed on FPSController so basically the camera moves when the player presses shift. Though the animator works like the animation plays just but I can’t use the animation component only the animator one.

bump.

how many animations are attached to the “FPSC”? GetComponentInChildren will stop at the first one it finds, if you have multiple animation components attached to it it might not be finding the one you are looking for?

Nope the FirstPersonCharacter has no animation component.