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 ClearRoseOfWar · Dec 16, 2015 at 03:30 AM · programmingstringsbooleansconverting

Check if a boolean is true by use of a string

Im trying to figure out a way to check if a boolean is true, but through a string.

Something like this:

 bool Boolean = true;
 String TempStr = "Boolean"
 if(UseAsBool(TempStr) == true)

Basically it would be doing this:

 if(Boolean == true)

Any help with this would be greatly appreciated :)

Comment
Add comment
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

2 Replies

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

Answer by ClearRoseOfWar · Dec 17, 2015 at 12:13 AM

(DEC 19 2015)

The answer is simply using the name of the sprite to do the check, and then also check if the boolean is true.

I wanted it to be a bit less code, but its not taking up that many lines of code, so its fine.

Answer:

 if(tempstr == "Nameofimage" && Nameofimage == true) //Be Happy


(DEC 17 2015)

Thank you very kindly :) I am pretty sure that this doesn't happen often. This solution would be perfect (Everything works great) if I already knew ahead of time what boolean I'd replace myBool with. However, I am doing something a little bit more strange. I`ll try to explain this the best I can :P

Inside my assets folder, I have the sprites, which are named:

%|-781188308_1|% %|-284706502_1|% %|-90134012_3|% %|1940178792_2|%

I also have 50+ booleans named after their corresponding sprites names`.

 ImageType0 = true;
 ImageType1 = false;
 ImageType2 = true;
 ImageType3 = false;

The sprites are images, that can be viewed if they`ve been unlocked. If the image is not locked, then its corresponding boolean is true. (meaning its been unlocked)

Now when I set it up to select a random sprite, It needs to be able to check if its been unlocked or not yet, so I tried this:

 stringBool = System.Convert.ToString(GameController.Temp_Sprite.name);
 if(stringBool== "True") {
     print("True");
 }
 else {
         print("False");
 }

I came to the conclusion that its always going to be false, since the stringBool is just the name of sprite.

How can i get the value of ImageType0 (the bool), simply by using the image's name that is randomly selected

Thank you for you help with this @Knnthra. As you can see, its not entirely trivial...

Cheers. DLively

Comment
Add comment · Show 4 · 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 Soraphis · Dec 17, 2015 at 12:27 AM 1
Share

wow, your doing some strange things here. first of all: posting an reply as answer to your question :P

Lets see: You could make a class ImageBooleanPair holding an image asset and an boolean, is serializable and so on (maybe as scriptable object or so). So you could use a 50+ elements array for this.

but to answer your question

How can i get the value of ImageType0 (the bool), simply by using the image's name that is randomly selected

reflections bool b = (bool) typeof(YourClassWhichHoldsAllTheBooleans).GetField("FieldName").GetValue(null);

avatar image ClearRoseOfWar Soraphis · Dec 19, 2015 at 12:29 AM 0
Share

Hi @Soraphis, Thanks for your reply, time and idea!

Terribly sorry for the latish reply. I am a hobbyist who works every day.

Indeed this may seem strange -> But I am making a puzzle game which has many images to select from.. I guess I should have said that from the start :)

Only two images per category are available from the start of the game. Once you complete any one of the two first available puzzles, the third becomes available.

This process is done with booleans.

there are 50+ puzzles, which means 50+ booleans.

When "Random" Is selected, a random puzzle is chosen by a random puzzle selection algorithm.

Before it can load that puzzle, its needs to check if it can or not.

This is where the "IF" statement comes in. It needs to check if that randomly selected puzzle's boolean is true or false.

so I get the name of the selected puzzle stored into a string, where I'd then like it to check if its boolean equivalent is true or false to deter$$anonymous$$e if its needs to select another random puzzle or not.

I see the route you have suggested, and I think I understand what you're saying. I now have a plan B :)

What I'd really like to know first, is if I can take the string contents of stringbool to enable / disable its boolean equivalent. (Can this be done?)

 if(UsingAsBoolEquivalent(stringbool) == true) //Be happy

If it can't be done then I can move forward with plan B, however I don't want to scrap it just yet until I know for sure.

Again, Thanks for your help guys! Cheers!

DLively

avatar image Soraphis ClearRoseOfWar · Dec 19, 2015 at 10:35 PM 0
Share

In the FieldInfo class of the reflection framework is beneath the method GetValue(), a method SetValue.

but i have a addition to my comment above: The last parameter: if your boolean fields are not static, replace null with the reference to your class-who-holds-the-boolean-values object.

with GetValue, you'll get the value not the reference to the field (afaik), to change this value you'd need to use the SetValue method

edit: but using reflections in this kind of way may not be the intention behind reflections :P i'd really recommend checking first if plan B would'nt be a cleaner-programmed solution.

Show more comments
avatar image
1

Answer by Knnthra · Dec 16, 2015 at 07:33 AM

I don't know why you would ever do this, but you could try something like this :)

 bool myBool = true;
 string stringBool;
 stringBool = Convert.ToString(myBool);

 if (stringBool == "true")
 {

 }
Comment
Add comment · 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

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

35 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

Related Questions

Populate Custom List with Csv items 1 Answer

how can i change game to false when the conditions are met in the winner() function? 0 Answers

Animation problem? 0 Answers

My player doesn't take fall damage ... please help 0 Answers

How to copy and paste animation events from one clip to another if those clips are included in FBXs? 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