- Home /
How to use GetChild in Loops and Traversing
Hello. I am working on a basic feature but am having trouble with the logic needed.
I have five buttons, and when you click on Three, it is set to active as well as the previous two. When you click on Five all five are active. Then when you click back on Two, only the first two are active.
So what I have in mind is taking the ID of the selected button and reference that in a for loop to set the previous children to active and the following children as inactive.
My hierarchy is as follows:
Datamarks Block - DatamarksOne - Checkbox - DatamarkTwo - Checkbox ...
So if I click on the Third datamark, how can I find the other children checkboxes to adjust their alpha property?
for (var x : int = 0; x < value; x++) {
}
Answer by NoseKills · Dec 11, 2014 at 05:06 PM
You are not saying how you keep track of the clicked button etc. but the optimal situation would be for example to have all buttons in an array and the clicked button in a variable:
GameObject[] allButtons;
void setButtonStates(GameObject pressedButton)
{
bool found = false;
for (var x : int = 0; x < allButtons.Length; x++)
{
if (found)
{
// clicked button was found earlier.
// This is a button after the selected one
// set allButtons[x] to unselected state
}
else // !found
{
// clicked button is still ahead or this one
// adjust alpha of allButtons[x] to selected value
}
found = allButtons[x] == pressedButton;
}
}
Your answer
Follow this Question
Related Questions
Menu Items as checkboxes/radio buttons 4 Answers
Physics.CheckBox() Alternatives 0 Answers
how to create checkbox preference regarding image effects?? 0 Answers
C# Script Haven't Check Box 2 Answers
CheckBox in unity3d 1 Answer