Using GUIToggle to on/off buttons!

Evening, everyone! I have another question regarding the GUIToggle function. Here’s my current code thus far:

var toolSkin: GUISkin;
var toggle = true;

function OnGUI () {
	
	GUI.skin = toolSkin;
	
	//Top Left Button
	toggle = GUI.Toggle (Rect(50,400,100,100), toggle, "");
	
	//Middle Button
	toggle = GUI.Toggle (Rect(120,440,100,100), toggle, "");
	
	//Top Button
	toggle = GUI.Toggle (Rect(120,360,100,100), toggle, "");
	
	//Top Right Button
	toggle = GUI.Toggle (Rect(190,400,100,100), toggle, "");
	
	//Bottom Button
	toggle = GUI.Toggle (Rect(120,520,100,100), toggle, "");
	
	//Bottom Left Button
	toggle = GUI.Toggle (Rect(50,480,100,100), toggle, "");
	
	//Bottom Right Button
	toggle = GUI.Toggle (Rect(190,480,100,100), toggle, "");
}

Basically, what this will show is 7 buttons which will change when it is in normal state, hover state and active state. However, once I click on an individual button, all 7 buttons light up at once. How do I prevent this from happening and give me a result whereby clicking an individual button will only cause that button to light up and clicking another button will cause the previous lighted button to off and the current button to light up?

Any help given will be gladly appreciated, thank you very much!

Byerdelen is right, one toggle = one boolean. So if you are clicking on any of your buttons you are changing the same boolean “toggle”.

You could create 7 booleans and link each button to its own boolean like so

private var toggle1 : boolean = false;
private var toggle2 : boolean = false;

toggle1 = GUI.Toggle (Rect(10,10,100,100), toggle1, "");

toggle2 = GUI.Toggle (Rect(110,110,100,100), toggle2, "");

You must have one boolean for each button, and set only the clicked one, clearing all the others. In the script below, only one button is active at any time.

var bt1 = false;
var bt2 = false;
...
var bt7 = false;

// Clears everybody and returns true to help setting the desired one
function setMeOnly():boolean{

  bt1 = bt2 = bt3 = bt4 = bt5 = bt6 = bt7 = false;
  return true;
}

function OnGUI () {

  GUI.skin = toolSkin;

  //Top Left Button
  if (GUI.Toggle (Rect(50,400,100,100), bt1, "")) bt1 = setMeOnly();

  //Middle Button
  if (GUI.Toggle (Rect(120,440,100,100), bt2, "")) bt2 = setMeOnly();

  ...

  //Bottom Right Button
  if (GUI.Toggle (Rect(190,480,100,100), bt7, "")) bt7 = setMeOnly();
}

If you want to be able to turn the button ON and OFF while keeping the radio button behavior, use this OnGUI function instead:

function OnGUI () {

  GUI.skin = toolSkin;

  //Top Left Button
  bt1 = GUI.Toggle (Rect(50,400,100,100), bt1, "");
  if (bt1) bt1 = setMeOnly();

  //Middle Button
  bt2 = GUI.Toggle (Rect(120,440,100,100), bt2, "");
  if (bt2) bt2 = SetMeOnly();

  ...

  //Bottom Right Button
  bt7 = GUI.Toggle (Rect(190,480,100,100), bt7, "");
  if (bt7) bt7 = setMeOnly();
}

Here’s what I would do:

I would create a new class to represent a custom toggle button.

The new class would keep track of the button’s position, label, current state, and maybe even the name of the style it uses.

I would then create an array of these objects and iterate over that array to draw the buttons.

 class ActionButton
 {
   var styleName   :  String;   // name of custom style in GUI Skin
   var isSelected  :  boolean;  // is the toggle button on or off
   var label       :  String;
   var position    :  Rect;     // where to draw this button on the screen 
 }


 /* an array of buttons that you can populate in the inspector */

 var buttonList  :  ActionButton [];


 function OnGUI ()
 {

   for ( var i = 0; i < buttonList.length; i ++ ) {

     var butt = buttonList*;*

butt.isSelected =
GUI.Toggle(butt.position, butt.isSelected, butt.label, butt.styleName);

if ( butt.isSelected ) {
print("This butt is turned on: " + butt.label);
resetOtherButtons(i);
}
}
}

function resetOtherButtons ( ignoreThisNumber )
{
for ( var i = 0; i < buttonList.length; i ++ ) {

if ( i != ignoreThisNumber ) {
buttonList*.isSelected = false;*
}
}
}

No worries… Glad it helped (and thanks for the tip)!

Hi guys, I may be out of date, but I run into this problem as well and come up with a simple ideia that I want to share with you…

int toggleGrid(int currentSelected, string [] selectionOptions) {
	int index = 0;
	int selected = currentSelected;
	foreach (string oneOption in selectionOptions) {
		if (GUILayout.Toggle(currentSelected == index, oneOption) 
			&& currentSelected != index) {
				selected = index;
			}
			++ index;
		}
		
	return selected;
}

and then use like this:

selectedOne = toggleGrid(selectedOne, new string[]{"option1", "option2", "option3", "option4"});

I know you can use the selection grid option for the same purpose (GUILayout.SelectionGrid(selectedOne, new string{“option1”, “option2”, “Option3”, “Option4”}, 1, “toggle”):wink:

But I needed something that I could modify at my will, so I can add some features…(enable/disable individual option, for example)

That’s it =)