moshe ralf moshe ralf - 28 days ago 7
C# Question

Why the GameObject.animation does not have the play property?

using UnityEngine;
using System.Collections;

public class Lift : MonoBehaviour {

private bool pressedButton = false;
private bool isElevatorUp = false;

public GameObject target;

void OnMouseOver()
{
pressedButton = true;
}

void OnMouseExit()
{
pressedButton = false;
}

void OnMouseDown()
{
if(isElevatorUp == false)
{
target = GameObject.Find("Elevator");

}
}

void OnGUI()
{
if(pressedButton == true)
{
GUI.Box(new Rect(300, 300, 200, 20), "Press to use lift!");
}
}
}


When i type target.animation.
Then after the last point i see some properties but not play.

It should be: target.animation.Play but the Play is not exist.

Answer

Direct animation play function call on a GameObject such as GameObject.PlayAnimation(...) is now deprecated. You must use GetComponent to get the Animation component then call the Play() function afterwards.

target.GetComponent<Animation>().Play();

There is also Animator. If you are using the Animator instead of Animation:

target.GetComponent<Animator>().Play("animationState");