- Home /
bool to int? (false = 0, true = 1) Conversion possible? Or manually set int to 0/1?
Hi.
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.
Thanks,
Luka.
Answer by Razion · Nov 24, 2012 at 06:44 AM
You didn't specifiy which language you're using, but in C# it's doable using the standard Convert.ToInt32(bool) method.
C# that is, I forgot to mention. Thank you very much, got the answer I wanted!
bool.GetHashCode();
At the top of the cod you need to be Using System;
, otherwise you have to use System.Convert.ToInt32(bool)
Answer by Anxo · Jun 18, 2014 at 04:34 PM
Also b/c you are only dealing with 2 states, you could just say
bool mybool = false;
int boolInt = mybool ? 1 : 0;
This is how I sued it.
if (PlayerPrefs.Has$$anonymous$$ey ("usetilt")) {
useTiltControl = (PlayerPrefs.GetInt ("usetilt") == 1) ? true : false;
}
You don't even need the if-else. (PlayerPrefs.GetInt ("usetilt") == 1) returns a boolean value already.
Your answer
Follow this Question
Related Questions
how to arrange numbers 1 Answer
Render Numerical Values w/o Using GUI.* 1 Answer
hierachy order is not in any order 2 Answers
Multiple error without line number 0 Answers
Random.Range(..) not working 1 Answer