- Home /
The question is answered, right answer was accepted
determine what button was clicked?
I have a script that Takes the elements of a string[] and makes them into GUI Buttons i was wondering how could i determine what GUI button was clicked? Heres my script:
public string CurrentMenu;
public string[] AmountOfButtons;
void Menu()
{
var names = QualitySettings.names;
names = AmountOfButtons;
GUILayout.BeginVertical ();
GUILayout.BeginArea (ButtonRect);
for (var i = 0; i < names.Length; i++)
{
if (GUILayout.Button (names[i]))
{
AmountOfButtons.ToString();
CurrentMenu = AmountOfButtons[0];
print ("Clicked 1st button");
}
}
GUILayout.EndVertical ();
GUILayout.EndArea();
}
Answer by ArkaneX · Jan 06, 2014 at 12:53 AM
If you want to know if you clicked the 1st, 2nd, etc. button, then you can use i
variable which you use to iterate through your string array.
if (GUILayout.Button (names[i]))
{
print("Clicked button number: " + i); // please note that this is 0 based, so if you want to start from 1, you have to use i + 1
}
If you want to store this index and reference it later in your code, you can create additional variable clickedButtonIndex
in your class, and assign to it the index of a clicked button:
if (GUILayout.Button (names[i]))
{
clickedButtonIndex = i;
}
Follow this Question
Related Questions
3D Button? 1 Answer
GUI.enabled ? 0 Answers
How Do I Center A GUI Label? 5 Answers
UI elements problem 0 Answers
how can i animate a camera to move when the level is starting ? 1 Answer