GUI.Toggle to only send one action, not repeated

Hi people,

I’ve set up a script with a number of toggle buttons. I have them switching on and off when clicked which is fine. When clicked, a function is performed, but it keeps sending the message. What I want to do is just send a single message/action once. This is obviously a standard button action, but I couldn’t figure out how a button could have an on/off state; change graphic when touched.

Here’s my code:

#pragma strict

var LeftNavSkin: GUISkin;
//var toggle = true;

var bt1 = false;
var bt2 = false;
var bt3 = false;
 
// Clears everybody and returns true to help setting the desired one
function setMeOnly():boolean{
 
  bt1 = bt2 = bt3 = false;
  return true;
}
 
function OnGUI () {
 
  GUI.skin = LeftNavSkin;
  
  GUI.Box (Rect (-30,10,100,90), "");
 
  //Top Left Button
  if (GUI.Toggle (Rect(50,300,100,100), bt1, "")) bt1 = setMeOnly();
  
 
  //Middle Button
  if (GUI.Toggle (Rect(50,410,100,100), bt2, "")) bt2 = setMeOnly();
 
  //Bottom Right Button
  if (GUI.Toggle (Rect(50,520,100,100), bt3, "")) bt3 = setMeOnly();
		
//This sends the debug.log repeatedly. I want it to just send once.

if(bt1 === true)
{
   Debug.Log("OMG IT WORKS");
}

}

Help would be appreciated!

Rich

If you only ever have one button on at one time, you can use a SelectionGrid with a custom style. Define your Style in a GUISkin. You can set the different textures for when the button is pressed/hovered over etc. via the inspector.

As for your function problem, after the SelectionGrid, use if(GUI.changed) MyFunction(); Alternatively keep two ints, one for the old value, one for the new and then check each frame if the old value matches the new one.