- Home /
 
 
               Question by 
               MrGravyCakes · Dec 01, 2014 at 01:06 PM · 
                c#animationdoorkeypress  
              
 
              Open Door on Keypress Not Working - Animations
I have added the animations and done all the code but the door will not open. Please could you have a look at the code and see what is happening? C# please :)
using UnityEngine; using System.Collections;
 public class DoorControl : MonoBehaviour {
 
     private Animation _anim;
     public bool doorOpen;
 
     
     void Start()
     {
             
 
     }
     
     
     void Update ()
 
     {
 
     if(Input.GetKeyDown(KeyCode.Q))
         {
         OpenDoor();
         }
 
     }
 
     void OpenDoor()
     {
         if (doorOpen == true) {
             _anim.Play ("ClosingAnimation");
             doorOpen = false;
         }
             else
             {
                 _anim.Play("OpenAnimation");
                 doorOpen = true;
             }
         }        
     }
         
     
         
 
 
 
 
              
               Comment
              
 
               
              Answer by jenci1990 · Dec 01, 2014 at 01:58 PM
Insert to Start method:
 _anim = gameObject.GetComponent<Animation>();
 
               but you can play it without _anim variable:
 gameObject.animation.Play("AnimName");
 
              Your answer