C# GUI SelectionGrid How to Assign Values

Could someone please tell me how to assign values to the buttons when using a GUI.SelectionGrid. I’m using the code from Unity - Scripting API: GUI.SelectionGrid. What I want to do is when I click the 1st button an attack will happen, click the 2nd button and the characters health will be be full again and so on. I have the selection grid on screen but cant figure out how to do actions when the button pressed. The code from the website is -

public int selGridInt = 0;
    public string[] selStrings = new string[] {"Grid 1", "Grid 2", "Grid 3", "Grid 4"};
    void OnGUI() {
        selGridInt = GUI.SelectionGrid(new Rect(25, 25, 100, 30), selGridInt, selStrings, 2);
    }

Thank you

should be something like this

    public int selGridInt = 0;
        public string[] selStrings = new string[] {"Grid 1", "Grid 2", "Grid 3", "Grid 4"};
        void OnGUI() {
            selGridInt = GUI.SelectionGrid(new Rect(25, 25, 100, 30), selGridInt, selStrings, 2);
            if (selSearchInt == 0) {
            // execute code
            }
            else if (selGridInt == 1) {
            // execute code
            }
            else if (selGridInt == 2) {
            // execute code
            }
            else if (selGridInt == 3) {
            // execute code
            }
        }

I would guess that selection grid is not what you really want
with the selgrid, you cant use the same button twice in a row, so no attack attack attack etc

hpjohn is correct because OnGUI will be call several times in a frame kinda like the Update function so if you have your selection grid 0 int active it will stay active as long as the button is selected. You could do a switch statement or use booleans for what your trying to do.

Thanks, I was trying to change change if (selStrings == 0), silly me :slight_smile: . I have little to no experience with switch cases so i will use the boolean. But how do I use the boolean with it properly? My if block is this

if(selGridInt == 1  stopbutton == true){			
			Debug.Log ("Button 1" + stopbutton);
			stopbutton = false;
		}

Where is the best place to set the boolean back to true? Sorry if its a stupid question, I am still new to unity and coding

I have to apologize jamie1789 I just re-read what you want the script to do and I was wrong you don’t have to use a boolean or a switch statement.

What you want to do is very easy just do a simple GUI button.
Example:

void OnGUI() {
	if (GUI.Button(new Rect(25, 25, 50, 30), "Grid 1"))
	{
		Attack();
	}
	
void Attack(){
	// execute attack code
	Debug.Log("we have executed the attack function");
}

or

public bool increaseHealth = false;

void OnGUI() {
	if (!increaseHealth  GUI.Button(new Rect(25, 25, 150, 30), "Health Increase"))
	{
		increaseHealth = true;
	}
	else if (increaseHealth  GUI.Button(new Rect(25, 25, 150, 30), "Stop Health Increase"))
	{
		increaseHealth = false;
	}
}

void Health() {
	// execute attack code
	Debug.Log("we have execute the health function");
}

void Update() {
	if (increaseHealth)
	{
		Health();
	}
}

Thanks for the replies