can't play simple animation

I’ve been searching the web for 4 hours but I still haven’t figured out how to play a single #*!$#-ing animation on my GameObject. Help pls…

using UnityEngine;
using System.Collections;

public class DuelPlayerControllerX : MonoBehaviour 
{

	/*
	public AnimationClip attackTwoAnimation;
	public AnimationClip attackThreeAnimation;
	public AnimationClip dieOneAnimation;
	public AnimationClip dieTwoAnimation;*/
	public AnimationClip attackOneAnimation;



void  Awake ()
{
	
}


void  Update ()
{
	
		if(Input.GetKeyDown (KeyCode.U))
		{
			Debug.Log ("U key pushed");
			animation.Play("attackOneAnimation");
		}

		
}

}

How old is the model and have you updated to a newer unity since importing it? You may need to check import settings (legacy vs newer methods). You're getting the output "U key pushed" correct?

hi HalversonS, Yes, when I push U, the output says it has been pushed. The settings are set to legacy. And yes, I have the Unity updated.

Does the gameObject play the animation clip if you select it from the animation window within the editor? Also, are you getting any errors or is it just not playing the animation?

You're code is all fine. Plays animations on my objects (I'm a java guy so I had to check for myself, can't just eye it). It'll have to be either an import setting or something in the editor.

2 Answers

2

Check that play on awake is not checked for your animations, as you are triggering them via code. Also check that the animations are set to loop (assuming you want to do this) from the import settings.

Play automatically won't matter as will if it's set to loop or not.

I dropped your script on a simple door of mine. -Fonted as Code Sample so you can read this easier without me spending 20 minutes editing it, none of this is code ;)-

Here are the settings I have (it plays just fine, as many times as I press ‘U’)

Inspector components

Make sure your script is added to the object with the animations. The animations will be on the parent of the fbx file, so must the script be included there. You should see the animations next to Elements, the name there must match your script.

Play Auto- doesnt matter
Animate physics (mine are off)

culling type - always animate (mine is set to this, you may need it based on renders depending on what you’re doing.)

Import settings:
-RIG-
Animation Type: Legacy
Generation: Stored in Root (deprecated)

-Animations-
Import ani - check
Wrap mode- default
anim. compreeesion - keyframe reduction
errors all set to .5

clips
-----
names with start to end. (door_open) is the name of the animation, so in script
animation.Play("door_open");

Add loop frame - unchecked
wrap mode - default

If that doesn’t work there is an issue with the export. Go back to maya/max/blender and re-export it and make sure the settings there are correct (such as animations being checked and the right amount of keyframes.

Also try adding a different object with a simple animation on it (cube rotating) and try it. Just to make sure it isn’t an issue with your editor.

Good luck!

thanks for the advices guys, I'll look at these tomorrow.

Check if your animation is imported as "Legacy". If I try to import as Generic, Humanoid or anytheing else than Legacy, it won't work for me.

Also, using a variable as an animation where the variable is a string, you wouldn't use quotations. So it would be just: animation.Play(attackOneAnimation);

Hi HalversonS, thanks for the suggestions but it's still not working. The animations and the script are on the same gameObject, so it should work, right? At least that's how I understand what you wrote above. Also, when I omit the quotes, an error message pops up: Argument #1' cannot convert UnityEngine.AnimationClip' expression to type `UnityEngine.PlayMode' ...so I still can't play the animations via the variables I created :(