Trick to OR Operator
Is it possible to doe something like:
if (PlayerPrefs.GetString ("Something") == ("OneThing" || "AnotherThing" || "ThirdThing")) {
}
Or would I have to do:
if (PlayerPrefs.GetString ("Something") == "OneThing" || PlayerPrefs.GetString ("Something") == "AnotherThing" || PlayerPrefs.GetString ("Something") == "ThirdThing") {
}
@Guhanesh Originally I was using '&&' but then I realized that that wouldn't have worked so I changed it to '||'. I just changed the title.
Answer by Alec-Slayden · Jun 02, 2016 at 09:04 AM
It may be a tiny performance hit, but if you're looking for quick testing you could put the valid strings into a list or array, and check if the array contains the value being tested.
For example, a simple filter might look like this:
public void SomeFunction()
{
string result = PlayerPrefs.GetString("MyStringValue");
List<string> goodResults = new List<string>()
{"good", "better", "best"};
if (goodResults.Contains(result))
{
// do good stuff
}
// optionally check other lists.
}
At the very least, I would cache the result of the player refs getter to save yourself some keystrokes and lookups, by storing it in a local variable like above with 'result' (though ideally more appropriately named).
[Edit]: If things start to get complicated you may want to also consider using a switch statement. You can fall through cases if they are left blank, allowing you to use it like a chained if / or series, as well as else-ifs.
switch(mystring)
{
case "optionA":
case "optionB":
case "optionC":
//do stuff if == optionA, optionB, or optionC
break;
case "optionD":
case "optionE":
// do stuff if == optionD or optionE
break;
case "optionF":
// do stuff if == optionF
break;
default:
// do stuff if none of the above
break;
}
Yeah, that is probably the closest to what I was hoping for. Thank you!
Your answer
![](https://koobas.hobune.stream/wayback/20220612082119im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity crashes when using while loop 0 Answers
While loop freezes game for 3 seconds then unfreezes (not infinite loop!) c# 1 Answer
Loop variables (Names of Inputfields / Labels) 1 Answer
*Solved* Loop instantiating objects causes my Unity Editor to crash 1 Answer
,Code in loop simultaneously executing on members of array rather than iterating one at a time 1 Answer