Combo box problems

Hi! I’m new to Unity and am working through C#, I’m hard-coding the UI (personal preference) and wanted to make a combo box, after a bit of searching I stumbled upon this video :

This video provides a download for the script, which I’m now using.

I have a number of issues that I’m not aware of how to fix.
The first issue is that when opening the drop-down menu, stuff behind it is still visible. I’d like that to not be the case.

The Second issue is that when attempting to select a choice (in this case mercenary for current occupation) you open a drop-down list BEHIND the current one, making some choices non-selectable.

My third and final issue is that given the script, I’m not sure how to take the player choice and save it/take an int variable from the choice and save it) - example : choosing knight for current occupation gives you +1 sword and +1 shield, I’d like to add these ints to an already existing int, playerSword/playerShield which outputs to a label.

Thus would make this
Sword 9

HERE’S THE CODE FOR THE DROPDOWN BOXES;

using UnityEngine;
using System.Collections;

public class helloWorld : MonoBehaviour
{
    public string[] items;
    public Rect Box;
    public string selectedItem = "None";

    private bool editing = false;

    private void OnGUI()
    {
        if (GUI.Button(Box, selectedItem))
        {
            editing = true;
        }

        if (editing)
        {
            for (int x = 0; x < items.Length; x++)
            {
                if (GUI.Button(new Rect(Box.x, (Box.height * x) + Box.y + Box.height, Box.width, Box.height), items[x]))
                {
                    selectedItem = items[x];
                    editing = false;
                }
            }
        }

    }

This is ‘correct’ behavior for UnityGUI Buttons - the first button to get drawn (ends up underneath others) is the first button that absorbs events.

Stick the ‘behind’ buttons into an if-statement so they just don’t get drawn when the combo is open

I’m new to programming and unity, so I have no idea how I’d go about doing that.

Well youve already got the combo options inside if(editiing){…}
put the ones you want to disappear inside if(!editing){…}

Treat me like you would a child who doesn’t know code.
You know those “For dummies” books ? They’re for people like me.

I don’t have an if (!editing), though I assume you mean for me to make one, in which case, what do I put in it?

“put the ones you want to disappear inside if(!editing){…}” is the line that throws me.

put the what inside?

I’m using the same script attached multiple times to the scene camera.

If I change the code for one, wouldn’t it affect all?

    using UnityEngine;
    using System.Collections;
   
    public class helloWorld : MonoBehaviour
    {
        public string[] items;
        public Rect Box;
        public string selectedItem = "None";
   
        private bool editing = false;
   
        private void OnGUI()
        {
         if(!editing){ ////////////  <--
            if (GUI.Button(Box, selectedItem))
            {
                editing = true;
            }
        } //////////// <--
   
            if (editing)
            {
                for (int x = 0; x < items.Length; x++)
                {
                    if (GUI.Button(new Rect(Box.x, (Box.height * x) + Box.y + Box.height, Box.width, Box.height), items[x]))
                    {
                        selectedItem = items[x];
                        editing = false;
                    }
                }
            }
   
        }

Doesn’t work.

Ah right, yeah. A static bool will work across all instances of a component, so:

    public string[] items;
    public Rect Box;
    public string selectedItem = "None";

    private bool editing = false;

    static bool hideMyButton = false; ////////////////// <--

    private void OnGUI () {
        if ( !hideMyButton ) { ////////////////// <--
            if ( GUI.Button( Box, selectedItem ) ) {
                editing = true;
                hideMyButton = true; ////////////////// <--
            }
        } ////////////////// <--

        if ( editing ) {
            for ( int x = 0; x < items.Length; x++ ) {
                if ( GUI.Button( new Rect( Box.x, ( Box.height * x ) + Box.y, Box.width, Box.height ), items[x] ) ) {
                    selectedItem = items[x];
                    editing = false;
                    hideMyButton = false; ////////////////// <--
                }
            }
        }
    }

But this is a fairly naive approach, it hides all other buttons whenever any combo is opened, including ones that are nowhere close. There’s a few ways to calculate the ones that should be affected, but none of them would be particularly simple.

Perhaps rearrange the combo to pop out to the side, so it never overlaps anything else?

I changed how I’m doing it. Thanks for the help though, comboBoxes just aren’t possible with minimal code. :stuck_out_tongue: