bool to int? (false = 0, true = 1) Conversion possible? Or manually set int to 0/1?


Correct me if I’m wrong, but as far as I know, booleans are basically a full number, like integer that can only be 0 (false) or 1 (true). The new Unity 4 Mecanim blend trees can pick up parameters (made in the ‘Animator’ window/tab), but unlike outside the blend tree, you are not able to check booleans. If I could do that, it would not be the actual boolean it would check for true or false, but instead check if an integer is 1 or 0.

I know I could do it like for example “if(someBool == true) someInt = 1;” and reverse, but I would like to know if there is any way to convert it directly, so that it could be used like for example: “someAnimator.SetInt(“SomeInt”, someBool.ToFloat());” (which will not work at all). I am assuming that a bool cannot be converted to a float or int using ToFloat(), but is there another way of doing so?

I see no problem with setting the integer manually, but I am very interested to know other solutions.



You didn’t specifiy which language you’re using, but in C# it’s doable using the standard Convert.ToInt32(bool) method.

See: C# Reference - Convert.ToInt32

Also b/c you are only dealing with 2 states, you could just say

bool mybool = false;
int boolInt = mybool ? 1 : 0;