- Home /
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 :)
 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");
     }
 }
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by SeasiaInfotechind · Nov 06, 2015 at 05:35 AM
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 @SeasiaInfotechind :)
Thank you very much! It works now :D So what do I do if I want to play another Animation to let the menu slide out? I tried this but it doesn't seem to work: using UnityEngine; using System.Collections;
 public class $$anonymous$$enuAnimS : $$anonymous$$onoBehaviour {
     bool $$anonymous$$enuOpen;
     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.Get$$anonymous$$eyUp ("space") && !$$anonymous$$enuOpen) {
             animator1.enabled = true;
             animator1.Play ("$$anonymous$$enueAnim");
             $$anonymous$$enuOpen = true;
         } 
 
         if (Input.Get$$anonymous$$eyUp ("space") && $$anonymous$$enuOpen) {
             animator1.Play ("$$anonymous$$enueAnim2");
             animator1.enabled = false;
             $$anonymous$$enuOpen = false;
         }
     }
 }
Answer by AnniJ · Nov 09, 2015 at 02:40 PM
Hi again :)
I figured the rest out on my own :D 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;
         }
     }
 }
 
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                