- Home /
One variable set TRUE when another must be false. TOGGLE
Hi!
I'm writing my toggle for choosing race while creating character. So i have bool races[] = new bool[3] array, ONE of it's element should become true and as one element becomes true, another elements of this array must be false; this checking takes place in GUI method, so if i write something like this:
void OnGUI()
{
if (races[0]) {races[1] = false; races[2] = false;}
else if (races[1]) { races[0] = false; races[2] = false; }
else if (races[2]) { races[0] = false; races[1] = false; }
}
it won't work for me, 'cause another 2 races are being set in UPDATE so they woudn't become true further. Help me please to solve my issue.. I thought it was an easy question but for some reason i can't figure out, how to deal with it. Thank u in advance
Answer by jodev · May 01, 2015 at 09:26 AM
I would probably use a function like this:
void SetActiveRace(bool[] races, int raceIndex)
{
for (int i = 0; i < races.Length; i++)
{
races[i] = i == raceIndex ? true : false;
}
}
You pass the array and the index you wish to set to true. We then simply go over each array element and toggle it to true or false, depending on whether the current element is the desired index. This method has the added advantage that you can make the race array as long as you want without having to rewrite the logic.
Thank u very much - it works!:) It was so easy
Oh, couldn't u prompt me one thing, how to connect this code with GUI.Toggle()?
if (GUI.Toggle(new Rect(0, 30, 100, 30), races[0], "Day"))
{
SetActiveRace(races, 0);
}
else if (GUI.Toggle(new Rect(0, 60, 100, 30), races[1], "Night"))
{
SetActiveRace(races, 1);
}
else if (GUI.Toggle(new Rect(0, 90, 100, 30), races[2], "Evening"))
{
SetActiveRace(races, 2);
}
I've tried smth like this but no
Try removing else from the last two if-statements. Otherwise these toggles would only be shown if the first one is checked.
Answer by YoungDeveloper · Apr 30, 2015 at 09:35 AM
That seems like a weird solution. Why not use constants, enum or simple integer witch would represent the index?
i've tried to think something yet i've cant figure out how to implement my task with index.. (i've tried to use enums///)
Answer by rohankad · Apr 30, 2015 at 10:26 PM
public enum characterEnum
{
first,
second,
third
}
public characterEnum charState;
Now set the characterEnum using, charState = characterEnum.xxx;
This will be easy for you.
But how i can make ONLY ONE STATE TO BE TRUE BY THE TI$$anonymous$$E??? i can't figure out how to use this structure for my issue
The state is represented by the value of the enum variable, which can only have one state at a time. rohankad's solution would make it logically impossible for it to be in more than one state at a time. Alternatively as others have pointed out you could just use an integer (if the states correspond to elements of some other array then this might make more sense than enums).
If your states really are mutually exclusive, then these are much better ways of doing it than using a boolean for each state and trying to make sure that only one of them is true at a time.