- Home /
Mutator method to change multiple bool variables?
Hi,
I'm trying to make a method to change the state of whichever boolean variable's name(or index etc depending on easiest way to do it) is parsed into the method.
For example (The aim is to make situation1 in the second class true).
'Extract of First Class
globalStats.GetComponent<GlobalStats>().ChangeBoolean(situation1);
'Extract of Second Class
private bool situation1;
private bool situation2;
private bool situation3;
public void changeBoolean(string boolName)
{
???
boolName = true;
???
}
But that obviously comes up with a "can't convert string to bool" error.
I also tried to make an array containing all boolean variable names and just parsing in an index. But that encounters similar conversion issues.
How should I handle the updating of these boolean variables (Avoiding the use of 50+ if statements)?
There's almost always a better way than using string variable names. A common one is putting them in an array and using the index as the "name." Another, if you can only be in a single "situation," is having a single int with 50 legal values (and no bools.)
Echoing $$anonymous$$ike15's comment, might help if you back up a step to what you're trying to do.
Answer by Mikea15 · Jan 10, 2014 at 01:33 PM
Hi.
Can't you just pass it as a bool parameter??
public void changeBoolean( bool boolName )
{
// if negative => sets to positive. vice versa.
boolName = !boolName;
}
I think that will just send in "true" or "false" not the actual variable name. Also I would need to declare every boolean in the first class.
I'm having trouble understanding what you want to accomplish. Could you explain a little more what you pretend?
Answer by Related-Sloth · Jan 11, 2014 at 08:18 AM
Sorry for the poorly explained question.
The solution was very simple once I had another look at the unity manual.
Simply
globalStats.GetComponent<GlobalStats>().situation1 = true;
No use of mutator methods required :D.