- Home /
Convert String to TrueString
I need this to return a true or false
levelUnlockValue = bool.Parse(previousLevel);
The string is entered in the editor and is "LevelControl.control.level_01" and it equals true, but I get a format exception because I'm not using "true" as my string.
So I need a way to access the true value of "LevelControl.control.level_01" when it's entered as a string.
Any particular reason as to why it has to be that exact string?
Yes, because I need a unique variable for about 100 levels, and I don't feel like writing the unlock conditions 100 times.
because I need a unique variable for about 100 levels
This already sounds like a bad idea in the first place. There are ways to use reflection to reflect / inspect your own types at runtime, however it's extremely slow, prone to typing errors and also bypasses any OOP encapsulation which can be a nightmare to debug if anything goes wrong.
As soon as you have to handle anything about the amount of 2 things you should consider using an array, List or any other type of collection.
Since we don't know anything about your project we can't really offer a specific workaround.for your case.
You could try:
levelUnlockValue = previousLevel == "LevelControl.control.level_01" ? true : false;
That's definitely headed in the right direction. It gives the desired result.
The issue is that "LevelControl.control.level_01" is a public string that is filled in via the editor, based upon which level-select button it is assigned to.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
GetComponent, set boolean to true but it willn't revert 3 Answers
c# Ignoring conditional statement? 1 Answer
Change a Variable with another script not working (C#) 4 Answers