Click on Animation script throughout the game
Hi.
I'm trying to write a script in which the animation is played by mouse click, but not to the same object, and so that I could apply it to all objects in the project. In fact a game consists of only those objects and write a million scripts for every occasion is silly, like my shameful script. Help, C # please, translated into Javascript is hard to me.
What I'd tired (too crappy, I know):
using UnityEngine; using System.Collections;
public class AnimationChange : MonoBehaviour {
public GameObject GameObject;
public AnimationClip Animation1;
public AnimationClip Animation2;
public Animation anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animation>();
anim.AddClip(Animation1);
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0))
GameObject.GetComponent<Animation>().Play("Animation2");
}
}
It may become clearer if I show replacing sprites script, but addition animation is right thing
public Sprite sprite_01;
public Sprite sprite_02;
public GameObject GameObject;
void Start ()
{
}
void Update () {
if (Input.Get$$anonymous$$ouseButtonDown (0)) {
this.GameObject.GetComponent<SpriteRenderer> ().sprite = sprite_02;
Not addressing your question here but...
public GameObject GameObject;
You shouldn't use GameObject
as the name for a variable. That's going to cause all kinds of confusion and problems.
Your answer
Follow this Question
Related Questions
How too climb a ladder with the FPS Controller correctly 0 Answers
OVR Walking Animation trigger. 0 Answers
Why can't I call a function within the same class? (Or: how does unity handle instancing?) 0 Answers
Rotate Player 90 degrees about its Y axis relative to the mouse being dragged between two angles 1 Answer
Adding prefab object on collision to a another script 0 Answers