- Home /
make gui appear on gui button click
this is the code i just want it to show the other buttons when i click click the inventory button. i am not having any errors so thats good just not showing it when i click it
var inbw = 0;
var inbh = 0;
var weaponhight = 100;
var armorhight = 125;
var clothinghight = 150;
var jewerlyhight = 175;
var bw = 500;
var size = 100;
var size1 = 25;
function OnGUI()
{
if (GUI.Button(Rect(inbw, inbh, size, size1), "Inventory"))
{
if (GUI.Button(Rect(bw, weaponhight, size, size1), "Weapons"))
{
}
if (GUI.Button(Rect(bw, armorhight, size, size1), "Armor"))
{
}
if (GUI.Button(Rect(bw, clothinghight, size, size1), "Clothing"))
{
}
if (GUI.Button(Rect(bw, jewerlyhight, size, size1), "Jewerly"))
{
}
}
}
Answer by oliver-jones · Sep 16, 2013 at 07:44 PM
This is actually quite a common mistake for beginners.
Currently, your inventory ("Weapons, Armour, Clothing ...") is within your:
if (GUI.Button(Rect(inbw, inbh, size, size1), "Inventory")){
Which means that if you are to press it, the Inventory list will show ... but only for 1 frame (I believe). You need to create a boolean to trigger your GUI Inventory list:
var inventoryActive : boolean = false;
function OnGUI(){
if (GUI.Button(Rect(inbw, inbh, size, size1), "Inventory")){
//Give the opposite value (true will = false, false will = true)
inventoryActive = !inventoryActive;
}
if(inventoryActive){
//Put your buttons in here
}
}
Try that :)
ill try them both 2marrow at school im making a small game to get into collage ill let u both know if they work
Your welcome, don't forget to tick the answer correct that helped you. Thanks :)
Answer by perchik · Sep 16, 2013 at 07:47 PM
The problem is that the if statement only executes when the button is pressed. I imagine if you click and hold the 'Inventory' button, your GUI might show up.
A way around this is to set some kind of flag or marker to tell it that you pressed the inventory button:
var showInv: bool = false;
function OnGUI()
{
if (GUI.Button(Rect(inbw, inbh, size, size1), "Inventory"))
{
showInv=true;
}
if(showInv){
if (GUI.Button(Rect(bw, weaponhight, size, size1), "Weapons"))
{
}
if (GUI.Button(Rect(bw, armorhight, size, size1), "Armor"))
{
}
if (GUI.Button(Rect(bw, clothinghight, size, size1), "Clothing"))
{
}
if (GUI.Button(Rect(bw, jewerlyhight, size, size1), "Jewerly"))
{
}
}
}
yours is good but it just didnt hide the gui after i clicked button again im sure i could have edited it easy but im 2 lazy lmao ty anyway its still a awesome script
Your answer
Follow this Question
Related Questions
GUI GameObject Button? 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Circle GUI Button, Javascript? 1 Answer
How to activate a button? 1 Answer