- Home /
How to find variable status?
So when I have 3 variable for example:
var variable1 : boolean = true;
var variable2 : boolean = true;
var variable3 : boolean = false;
How (in script, in real time) find the variable which have a false status?
Thanks in advance ;)
You mean that you are sure that at anytime in your code, only exactly one of the three variables would be false ?
In that case, you can have just :
if (!variable1)
{
...
}
else if (!variable2)
{
...
}
else if (!variable3)
{
...
}
But I'm not sure this is what you really want ...
You cannot "loop find" a variable or an object that isn't stored in a collection over which you can have code loop. All three of your variables are singles.
Put the booleans in a List, then call IEnumerable.FirstOrDefault to find the first boolean in the list whose value is false. I can write you an example, but it would be in C#, not UnityScript. Is that still of use to you?
Answer by amphoterik · Jul 23, 2013 at 02:45 PM
You will need to change your code up a bit to do what you want. First off, the variables will have to be stored in an array. Try the following:
bool [] values;
void Start()
{
values = new bool[3];
values[0] = true;
values[1] = true;
values[2] = false;
}
Then, somewhere in your code (wherever you need it) you can find the false variable like this:
foreach (bool value in values)
{
if(!value)
{
//this is the one you want;
}
}
If this helped you find the answer, please select it as the answer. It helps future readers and gives you karma.
Your answer
Follow this Question
Related Questions
Toggle Boolean Function 2 Answers
Boolean not switching? 2 Answers
Change a Variable with another script not working (C#) 4 Answers
Yield Waitforseconds not working at all 3 Answers
ANOTHER Boolean Problem 1 Answer