1
votes

I have several gameObjects in my scene, let's say two: Button and Controller(empty GameObject); I keep all the code in the script attached to Controller, this script contains method onButtonClick() which should start the animation of Button. Button already has Animator component with working animation(when it is ticked, animation starts automatically after starting the game). How can I launch this animation from Controller on button click? Thank you.

1

1 Answers

1
votes

You must add the reference of the GameObject in your script and via inspector assign it.

In your controller script:

public GameObject obj;
public Button myButton;


void Start(){
     myButton.onClick.AddListener(()=> {
     obj.GetComponent<Animation> ().Play ("myAnim");
});

}

Doing that you will be ok.

PS: Dont put all your code in a single script, I advise you to learn about Single-responsibility Principle, this way you will keep your code simple and easy to fix in future bugs