Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
8
Question by LukaKotar · Nov 24, 2012 at 06:32 AM · boolnumber

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Nov 24, 2012 at 09:07 AM 0
Share

nice question

avatar image LukaKotar · Nov 24, 2012 at 09:10 AM 0
Share

Thank you!

2 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

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.

See: C# Reference - Convert.ToInt32

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LukaKotar · Nov 24, 2012 at 06:57 AM 0
Share

C# that is, I forgot to mention. Thank you very much, got the answer I wanted!

bool.GetHashCode();

avatar image tinylord202 · Oct 09, 2019 at 08:59 PM 0
Share

At the top of the cod you need to be Using System;, otherwise you have to use System.Convert.ToInt32(bool)

avatar image
9

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;
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Anxo · Jun 18, 2014 at 04:41 PM 0
Share

This is how I sued it.

 if (PlayerPrefs.Has$$anonymous$$ey ("usetilt")) {
      useTiltControl = (PlayerPrefs.GetInt ("usetilt") == 1) ? true : false;
 }
avatar image hatuf · Jun 18, 2014 at 04:54 PM 1
Share

You don't even need the if-else. (PlayerPrefs.GetInt ("usetilt") == 1) returns a boolean value already.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges