- Home /
How can i reverse all the booleans in a method?
ok im not really sure how to ask this but maybe someone can help.
I would like to reduce one of these blocks of code ..
void NotPressed ()
{
for (int i = 0; i < DangerousObjects.Count; i++)
{
isGroundButtonPressed = false;
DangerousObjects[i].SetActive(true);
animator.SetBool("GroundButtonOn", false);
}
}
void Pressed()
{
for (int i = 0; i < DangerousObjects.Count; i++)
{
isGroundButtonPressed = true;
DangerousObjects[i].SetActive(false);
animator.SetBool("GroundButtonOn", true);
}
}
so it can be something like this
void NotPressed ()
{
!Pressed();
}
void Pressed(bool isPressed)
{
for (int i = 0; i < DangerousObjects.Count; i++)
{
isGroundButtonPressed = true;
DangerousObjects[i].SetActive(false);
animator.SetBool("GroundButtonOn", true);
}
}
that doesnt work^ but im looking to do something like that.i dont need to do this -- i just want to know how someone one would go about doing that? is there a way i can flip all the boolean values to it's opposite value?
Answer by ajaykewat · Feb 19, 2020 at 08:39 AM
Hi @spencerz , I think you can try this
void NotPressed ()
{
SetProperty(false)
}
void Pressed()
{
SetProperty(true);
}
void SetProperty (bool value)
{
for (int i = 0; i < DangerousObjects.Count; i++)
{
isGroundButtonPressed = value;
DangerousObjects[i].SetActive(!value);
animator.SetBool("GroundButtonOn", value);
}
}
Note that the SetProperty method can be simplified to
void SetProperty (bool value)
{
isGroundButtonPressed = value;
animator.SetBool("GroundButtonOn", value);
for (int i = 0; i < DangerousObjects.Count; i++)
{
DangerousObjects[i].SetActive(!value);
}
}
There's simply no point in calling animator.SetBool multiple times. Likewise setting "isGroundButtonPressed" more than once is pointless.
Ahh I’m a dummy. Thank you for pointing that out!!
Answer by sacredgeometry · Feb 19, 2020 at 07:39 AM
To invert a bool you can use the !
operator so
MyVariable = !MyVariable
Will flip it from what ever value it was to the alternative value.
Your answer
Follow this Question
Related Questions
Scavenger Hunt List Bools Question 1 Answer
c# Ignoring conditional statement? 1 Answer
What does this mean? 1 Answer
Could use some help on making my runaway/chase/follow/patrol script cooperate with each other 1 Answer
my button dosent work, became a light switch instead of a normal button (like a bell) 1 Answer