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 :slight_smile:

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

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

if (stringBool == "true")
{

}

(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 :slight_smile:
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 :stuck_out_tongue:

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