- Home /
 
              This post is currently awaiting moderation. If you believe this to be in error, contact a system administrator. 
            
 
             
               Question by 
               bekah_mbrown · Apr 03 at 06:31 PM · 
                scripting problemfindenable and disable script  
              
 
              Enable/Disable Script on GameObject when pressing a key
Please help, I am trying to write a Script which enables/disables a Script (Called FPSPlayerLook) on a GameObject when the ESC key is pressed. When the Game is played and the ESC key is pressed nothing happens. I am unsure what is going wrong.
 public GameObject playerLook;
 
  void awake()
     {
         // The playerLook variable is looking for access to the GameObject with the "ResetPlayer" tag.
       playerLook = GameObject.FindWithTag("ResetPlayer");
     } 
 
 private void FixedUpdate()
     {
     if (Input.GetKey(KeyCode.Escape))
     {
         playerLook.GetComponent<FPSPlayerLook>().enabled = false;
     }
 
               }
               Comment
              
 
               
              Answer by miguelfrancisco1505 · Apr 03 at 09:06 PM
 [SerializeField] string KeyCode;
 [SerializeField] GameObject Thing;
 
 bool Activated;
 //was this update?
 bool WTU;
 
 //runs when a object with this class is loaded
 void Awake()
 {
      //sets initial state
      Activated = true;
 }
 
 //runs with the physics of unity
 void FixedUpdate()
 {
  /*checks if it was run this update, if it was switch from the state its currently in to the oposite one 
  sorry for bad english lol*/
      if(Input.GetKey(KeyCode) && WTU == false)
      {
           WTU == true;
           Activated = !Activated;
           Thing.SetActive(Activated);
      }
      if(!Input.GetKey(KeyCode) && WTU == true)
      {
           WTU = false;
      }
 }
 
              Your answer
 
             Follow this Question
Related Questions
CSharp script can't find th jv CharacterMotor !! 1 Answer
Turning Off Only One Instance of a Script 1 Answer
how to enable or disable a script via code c# 1 Answer
About takeover control 0 Answers
Canvas starts enabled no matter what 1 Answer