Can't play the animation

I have a gate, which consist of the left and right door. Each have animation of the opening: “LeftDoor” and “RightDoor”. When I come to the gate, the animations must play and open the doors.

But I’m getting an error mesage for both doors:

The animation state RightDoor could not be played because it couldn’t be found!
Please attach an animation clip with the name ‘RightDoor’ or call this function only for existing animations.
UnityEngine.Animation:Play(String)

Script is attached to the right door.

    public GameObject DoorL;
	public GameObject Player;
	public float minDist;

	void Update()
	{
		Vector3 doorRpos;
		Vector3 PlayerPos;
		
		doorRpos = gameObject.transform.position;
		PlayerPos = Player.transform.position;
		if (Vector3.Distance (doorRpos, PlayerPos) <= minDist)
		{
			DoorL.animation.Play ("LeftDoor");
			gameObject.animation.Play ("RightDoor");
		}
	}

30863-1.jpg

The problem is the animation type your using, I have this problem as well. What you will need to do is select the animation in your project view.

Click on little dropdown menu on the top left of your inspector and switch it to “Debug Mode”

30864-help.jpg

In Debug Mode change the animation type to 1 and this should work.

Hope this helps!

try to get the animation component attached to the gameObject then use that play animation

Animation obj = gameobject.getcomponent();

obj.play(“animationName”);

You have to add your animations in the “Animations” list of the Animation in the inspector. Those are the animations referenced by name; the “Animation” property (which you set as “LeftDoor”) is the default, it won’t be found by name.

Another option is to use just “animation.Play()”, without a name. That will play the default animation, in your case “LeftDoor”.