Activating GUI when key pressed

var distance : float;

var weaponUpgrader : GameObject;

var player : GameObject;

function OnGUI()


	distance = Vector3.Distance(weaponUpgrader.transform.position, player.transform.position);
   if(distance < 2.6)



   	//Cant figure out how the GUI enabling works here...


   if (distance > 2.6) 


   		print("Cant reach the object, text disabled");

   	  (guiText.GetComponent(GUIText) as GUIText).enabled = false;

      guiText.text = "This Text Is Now Disabled";



This is my script for players weapon box:
I need help with GUI enabling when you are near the object (if(distance < 2.6))
and when F is pressed, the GUI.Label,Box, or Button will open up, and when he presses F again, the GUI will disapper and “Weapon Upgrader
press F” will be enabled again.

Thanks for the help in advance! :slight_smile:

Check this other answer → GUI Visibility on/off …some other weirdness going on here too though, so… →

var closeRange : float = 2.6 ;
var weaponUpgrader : Transform ;
var player : Transform ;
var showGUI : boolean = false ;
var distance : float ;
function Update(){
   distance = Vector3.Distance(weaponUpgrader.position, player.position) ;
   if(distance < closeRange){ //if we're close, pressing F does something
         showGUI = !showGUI ;
   else{ //if we aren't close make sure gui is off
      showGUI = false ;
function OnGUI(){
   if(!showGUI) return ; //if gui isn't on, skip it

     //otherwise continue on with doing some gui stuff