Menu Animation

Hi guys,

I am trying to animate a UI menu and I’m having a little trouble with the script. I am trying to play the animation by pressing the space button, but I keep getting errors.
Could someone please look over my code and tell me what is wrong with it?

Thanks :slight_smile:

var animator : Animator;
var Panel : GameObject;
var MenuOpen : boolean = false;

function Start(){
	animator = Canvas.GetComponent("Animator");
	Canvas.Animator.enabled = false;
}

function Update () {
	if (Input.GetKeyUp ("space") && !MenuOpen){
		Canvas.Animator.enabled = true;
		Canvas.GetComponent.<Animator>().Play("MenueAnim");
	}
}

Hello Annij
Solution to your problem is :

using UnityEngine;
using System.Collections;

public class abc : MonoBehaviour {
	bool MenuOpen;
	Animator animator1;
	// Use this for initialization
	void Start () {
		animator1 = GameObject.Find("Canvas").GetComponent<Animator>();
		animator1.enabled = false;
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyUp ("space") && !MenuOpen)
		    {
			animator1.enabled = true;
			animator1.Play("New Animation");
			print ("abcdef");
		}
	}
}

Hi again :slight_smile:

I figured the rest out on my own :smiley: So for everyone who is looking this up this is my final script:

using UnityEngine;
using System.Collections;

public class MenuAnimS : MonoBehaviour {
	bool MenuOpen;
	Animator animator1;
	// Use this for initialization
	void Start () {
		animator1 = GameObject.Find("Canvas").GetComponent<Animator>();
		animator1.enabled = false;
		MenuOpen = false;
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyUp ("space") && MenuOpen == false) {
			animator1.enabled = true;
			animator1.Play ("MenueAnim");
			MenuOpen = true;
		} else if (Input.GetKeyUp("space") && MenuOpen == true){
			animator1.Play ("MenueAnim2");
			MenuOpen = false;
		}
	}
}