- Home /
Creating Dropdown GUI
I'm trying to create a dropdown GUI. This is my attempt:
if(GUI.Button(new Rect(200, 50, 120, 50), "Shapes")){
int[] nums = new int[containerOptions.shapes.Length];
int i = 0;
while(i < containerOptions.shapes.Length){
nums[i] = i;
i++;
}
foreach(int num in nums){
GUI.Button(new Rect(200, 100+(55*num), 120, 50), containerOptions.shapes[num].name.ToString());
}
}
After clicking a button called shapes, the dropdown list should come on. It creates an int array holding numbers from 0-shapes.Length, and then runs a foreach loop in which how far an item in the drop down list goes down is multiplied by its number in the list, therfore ensuring that each option is the same distance from the last and in order.
I've done something similar, and its worked before, but at the moment the GUI isn't showing up at all period. I've even made the size of it extremely large, and its not showing up. Anything I've done wrong here?
Answer by Seth-Bergman · Nov 13, 2012 at 04:05 AM
the if(GUI.Button ...etc only returns true the single frame the button is pushed.. you therefore cannot declare your other buttons INSIDE this loop, try something like this:
int[] nums;
function OnGUI(){
if(GUI.Button(new Rect(200, 50, 120, 50), "Shapes")){
if(!nums){
nums = new int[containerOptions.shapes.Length];
int i = 0;
while(i < containerOptions.shapes.Length){
nums[i] = i;
i++;
}
}
else
nums = null;
}
if(nums){
foreach(int num in nums){
GUI.Button(new Rect(200, 100+(55*num), 120, 50), containerOptions.shapes[num].name.ToString());
}
}
}
(I haven't tested this but I THINK it would work as written..)
maybe even easier, you could try THIS SCRIPT by Eric5h5
I've tried that and it doesn't work either. I used your code to see if maybe I was doing something wrong, but nothing showed up either.
Oh, I figured it out. The requirement for it to show was if toggle1 was true, but since it was declared false in OnGUI(), it always stayed as false, no matter what. Thanks for the help!
Your answer
![](https://koobas.hobune.stream/wayback/20220613083757im_/https://answers.unity.com/themes/thub/images/avi.jpg)