The input bool
is not being set to boolToSet
:
bool SwitchToOpposite(bool boolToSet)
{
if (boolToSet)
boolToSet = false;
else
boolToSet = true;
return boolToSet;
}
What do I need to do in order for it to return the boolToSet
?
Edit: here's the way I'm calling the method
public void OnFollowButton(InputAction.CallbackContext context)
{
if (context.performed)
{
SwitchToOpposite(pressingFollowButton);
}
}
If you want to set boolToSet
field or property within a class, use this.
prefix to address this field / property, e.g.
// Field within then class
private bool boolToSet;
// A method to change boolToSet field
bool SwitchToOpposite(bool boolToSet)
{
// We chech the argument passed
if (boolToSet)
// but change the field which belongs to object
this.boolToSet = false;
else
this.boolToSet = true;
// we return changed value, not the argument of the method
return this.boolToSet;
}
Note, that you can simplify the code:
// A method to change boolToSet field
bool SwitchToOpposite(bool boolToSet)
{
// Set (NOT boolToSet) to boolToSet field and return it
return this.boolToSet = !boolToSet;
}
If you want to change the static
field of property ("global"), you should use class name instead of this.
, e.g.
public class MyClass {
...
// Static ("global") field within then class
public static bool boolToSet;
...
}
...
// A method to change boolToSet field
bool SwitchToOpposite(bool boolToSet)
{
// We check the argument passed
if (boolToSet)
// but change the field which belongs to object
MyClass.boolToSet = false;
else
MyClass.boolToSet = true;
// we return changed value, not the argument of the method
return MyClass.boolToSet;
}