Hi, First I’m trying to play/stop an Animation clip with C# script but it didn’t worked but Javascript of that code worked (In legacy mode of animations). where is problem?
using UnityEngine;
using System.Collections;
publicclassPlayStop:MonoBehaviour
{
publicGameObjectCube;
voidPS()
{
GetComponent().Play();
}
}
var Cube : GameObject;
Cube.GetComponent.().Play();
Firstly, your code is extremely hard to read, so please refer to the forum tutorials on how to properly format your posts. To answer your question, one way to play an animation with the Legacy method would be like so:
using UnityEngine;
using System.Collections;
public class PlayStop : MonoBehaviour {
public Animation yourAnim;
public GameObject yourObject;
void Start() {
yourAnim = GetComponent<Animation>();
yourObject = GameObject.Find ("yourObject");
}
void PS() {
yourAnim.Play ("yourAnimation");
}
}
You grab the instances of the objects you need, and you call the animation in the PS method, by calling yourAnim.Play, with the name of your animation as a string. Make sure you include PS(); in your code somewhere, or the method will never activate.