Destroying Scripted Button (Javascript)

So I have been creating a very simple inventory script as I am new to scripting, and ran into an error. I have tried googling it countless times and came up with nothing. Here is the issue: I cannot hide the buttons after I create them. I am trying to get it to where I can press tab to open the inventory and escape to close it.

#pragma strict

var Can : GameObject;
var rock : float = 0;
var wood : float = 0;
var Open2 : boolean = false;
var open : boolean = false;
var a = GUI;
var b = GameObject;
function Update () {
    if (Input.GetKeyDown (KeyCode.Tab)){
    open = true;
    if (Input.GetKey (KeyCode.Escape)){
    open = false;
  
        }
    }
    if (open == false){
  
    }
}



function OnGUI () {
    if (GUI.Button (Rect (10, 10, 100, 20), "Inventory")){
        open = true;
        }
    if (open == true){
        var a : GUI = GUI.Button (Rect (10, 40, 100, 20), "Rock: "+rock);
        var b : GUI = GUI.Button (Rect (10, 70, 100, 20), "wood: "+wood);
    }
    if (open == false){
      
    }
}

And I am getting the error of:
Assets/MainInv.js(30,42): BCE0022: Cannot convert ‘boolean’ to ‘UnityEngine.GUI’.
Thanks in advance!

Instead of

var a : GUI = GUI.Button (Rect (10, 40, 100, 20), "Rock: "+rock);
var b : GUI = GUI.Button (Rect (10, 70, 100, 20), "wood: "+wood);

Do this way (because GUI is not a variable but a static class):

GUI.Button (Rect (10, 40, 100, 20), "Rock: "+rock);
GUI.Button (Rect (10, 70, 100, 20), "wood: "+wood);

And put them in the IF statement, otherwise why do you need a GUI.Button.

Ok, I have actually changed them to labels, because I was not thinking when I made this as I did not need buttons. Now I just need to know how to remove the Rock label and Wood label. Thanks for your help!

if(GUI.Button(Rect (10,10,100,20),"Inventory")){
    if(open==true){
        open = false;
    }else if(open == false){
        open = true;
    }
}

Now press Inventory button to toggle on/off the inventory.

Thank you so much! I am completely inexperienced in GUI, so it would of taken a long time for me to figure that out!

1 Like