Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Panchos · Jan 18, 2016 at 11:25 PM · stringbooleanbool

checking a bool via string

To save writing loads of code, I'd like to check if a bool is true based on the string "boolName", which can be changed on the fly.

Is there anyway of doing:

 string boolName = bool1;
 if (boolName == true) {
     print ("true");
     }

Thanks

UPDATE:

I discovered hashtables, which gave me a better way round of doing this. Thanks for the replies, helped me narrow down my options and reach a better solution

Comment
Add comment · Show 13
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 gjf · Jan 18, 2016 at 11:31 PM 0
Share

why on earth would you over complicate it like that? if it's a bool and can only be true or false, then use a bool.

is there a reason why you don't want to, apart from writing more code and making it slower?

avatar image Panchos · Jan 18, 2016 at 11:32 PM 0
Share

Because im doing a loop of about 20 items and i dont want to write 20 "if (bool1).. if (bool2) etc

avatar image gjf Panchos · Jan 18, 2016 at 11:35 PM 1
Share

if you've got a loop, then why not use a List or array of bools?

perhaps you can show me an example of your code to better understand why you want to do it that way. like i mentioned, it seems like you're making it more difficult for yourself...

avatar image Bonfire-Boy Panchos · Jan 19, 2016 at 12:05 AM 1
Share

Sorry but on the face of it this just seems crazy. How could what you're asking for simplify things? $$anonymous$$aybe I'm missing something. Like gjf suggests, it would be easier to understand if you were to show us how your loop would work.

avatar image DCordoba · Jan 19, 2016 at 01:24 AM 0
Share

After re-read your question, I think to you want to say to the string boolName will be true when contains the serie of characters "true", so you can use the Contains() method

 if (boolName.Contains("true")){
        print("true");
 }




avatar image gjf DCordoba · Jan 19, 2016 at 02:55 AM 0
Share

@DCordoba - i don't think that i misunderstood, but what the OP seems to be asking doesn't sound like a real solution to anything. if there was a better understanding of the use case, we'd be able to offer better advice.

...which is why i asked to see some code - checking strings is never going to be as simple/fast as a bool...

avatar image DCordoba gjf · Jan 20, 2016 at 01:38 AM 0
Share

@gjf yes, the bool is 8-bit, less than an extended character(32-bit), takes less memory and is more easy to process, but he not was asking about how to do well or improve that, is asking about exact solution. Not misunderstood me, is good help, but when somebody ask for this help, otherwise probably not will listen, just use other politic like "here you have what you looking for, but maybe not is the better way, try this way to no overheat in some simplest as that", but if you just say "that's trash (to say it better as possible) try this and all" probably not are helping nobody, just making someone to do his way just to go against you.

in other way, I think to the question is ambiguous, maybe he have to edit it, extend to an exact example of use, more specific to "is there anyway of doing:" and a code out of context.

Show more comments
avatar image NoseKills · Jan 19, 2016 at 07:31 AM 2
Share

The way i understand the question, it's the same one asked here every week: "how do i reference a variable based on it's name as a string?". Like:

 string boolName = "bool1";
 // and then have some way of checking
  if (valueOfBoolWithThisName(boolName) == true) {
      print ("true");
      }

And the answer i'd give is the same as previous weeks: yeah you can do stuff like that with reflection, but most likely you can avoid this whole problem by re-structuring your logic. And even if not, it would be better to make a Dictionary or an array of the booleans and reference the booleans from that with index (array) or $$anonymous$$ey string (dictionary)

avatar image Owen-Reynolds · Jan 19, 2016 at 07:25 PM 1
Share

This is actually a pretty common (deadend) idea for people who haven't seen arrays. The idea is usually to make vars named n1, n2, n3 ... and then use i an an index. You make string ("n"+i), then have to figure out how to map "n3" back to the n3 variable.

Of course, arrays are the real solution, and are much better.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

38 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[Help] Getting variable based on string value. 2 Answers

How to turn off/on a collider using a bool variable from another script 1 Answer

How to know how many bools is true in a list ? 0 Answers

If statement not working 1 Answer

Get Time A Bool Has Been True 4 Answers


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