Help! On/off button
Hi! Fairly new to C# but I know a fair amount.
The problem I have is assigning a key (in this case: x) to make it switch the Minimap camara on and off. I think the way I've coded it, its just running the on and off state's at the same time resulting in no change. How can I fix what seems like a really easy thing to get working?
Cheers!
using UnityEngine; using System.Collections;
public class MiniMapState : MonoBehaviour {
 Transform myTransform;
 public int mapState;
 // Use this for initialization
 void Start () {
     myTransform = transform;
     mapState = 1;
 }
 
 // Update is called once per frame
 void Update () {
     if(mapState == 1)
     {
     if(Input.GetKeyDown("x"))
         {
          GameObject.Find("Minimap").GetComponent<Camera>().enabled = false;
         }
         if(Input.GetKeyUp("x"))
         {
             mapState = 0;
         }
     }
     if(mapState == 0);
     {
         if(Input.GetKeyDown("x"))
         {
             GameObject.Find("Minimap").GetComponent<Camera>().enabled = true;
         }
         if(Input.GetKeyUp("x"))
         {
             mapState = 1;
         }
     }
 
 }
}
Answer by ElementalVenom · Sep 27, 2015 at 05:53 PM
Well if you wanted to create a toggle you could use a boolean and use just getkeydown to toggle the boolean as follows:
     bool toggleMe=false;
     
     void update(){
           if(toggleme){
               //toggle is on, so turn camera on
 }else{
              //Toggle is off, so turn camera off.
 }
          if(Input.GetKeyDown(KeyCode.X)){
               toggleMe=!toggleMe;
          }
     }
Thanks! Had no idea about bool.
Just for future understanding; what is this: toggle$$anonymous$$e=!toggle$$anonymous$$e?
Does having =! simply change the state of the boolean to its opposite? True - False or False - True?
Your answer
 
 
             Follow this Question
Related Questions
Cascading dropdown menus 0 Answers
Accessing string from other script 1 Answer
GetKeyDown not always registering 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                