var selectedOutfit : int = 0;
var checkInt : int = 0;
function OnGUI () {
GUI.BeginGroup (Rect (0, 0, 500, Screen.height));
selectedOutfit = GUI.SelectionGrid(Rect(10, 60, 480, 600), selectedOutfit, theButtonContent, 2);
GUI.EndGroup ();
}
function Update()
{
if (selectedOutfit != checkInt) {
print ("clicked: " + selectedOutfit);
checkInt = selectedOutfit;
}
}
When I run this I can click each button in the grid (there are 8 total), and as I click each one it prints in the console that it was clicked. So far so good.
The problem is, I can only get each button to register its click once. If I click it again it does nothing.
Can someone tell me what I’m doing wrong? Also, is there some better way to track clicks from a selectiongrid?
*(theButtonContent in my code is an array containing the GUIContent strings and is working fine.)
Are you saying that once you have selected an item, subsequent clicks on the same item aren’t registered? The SelectionGrid method can only report when the selected item has changed. You can use the Event to detect GUI mouse clicks. You could try checking each mouse click to see if it occurs within the SelectionGrid rectangle - if it does, but the selected item doesn’t change, then the currently selected item must have been clicked a second time.
Yes, that’s correct. If I click a second time nothing registers. I even did the code in several different ways to see if it was just my Update code, but each gave the same result. For instance, I tried doing a GUI.changed section inside the OnGUI and got the exact same thing: each button would register once, and that’s it. Subsequent clicks did nothing.
I’m going to mess around with it here some more including trying some things with an “event”, but if it ends up not working I guess I’ll have to just lay out a button grid by hand.
ADDED: Ok I just did some more testing and if I watch the Inspector window my variables are indeed registering each time I click a button, but the “print” code, or even Debug.Log code, will only print to the console once per click. So it appears that there isn’t an issue after all with the buttons in the grid registering, but rather an issue with the console only wanting to print a result for the first click of each one and then ignoring them after that. Weird.
In any case, at least I can see now that they are actually registering, so I’ll move on from here and test some functions with them.
Sounds like you have the “Collapse” option set in the console window. Make sure it’s not selected - I think that it’s on by default when you first install
That was it! Heh, thank you. I got the buttons working fine with the functions I was using them with but still the console would only log each hit once; the first time I did it. I turned off “collapse” and now each hit shows in the console just fine. Awesome.
Can you post your solution. I have a similar problem. I can detect everything but the fact I clicked on the same SelectionGrid button more than once. I’m using the SelectionGrid as a poor man’s combobox.
I know this is an old thread, but I’m bumping this old thread because I just spent hours trying to figure out the same problem and I’m guessing others have, too:
Using the manual’s “GUI.changed” example in the “Controls” section, the console would only print out that I clicked each button once. I thought maybe there was a bug with the code.
As 30harry said, the problem isn’t the code. It’s that the button labeled “Collapse” in the console window was checked, which evidently prevents messages from being duplicated.