- Home /
bool[string] = true; array possible?
My question is simple, is it possible to put a string variable in the array brackets versus the int requirement?
I am still fairly new to C# but everything that I've tried so far has failed. My goal is to be able to check if a character is unlocked, so:
if (unlocked[name] = true)
{
//execute unlocked code at select screen
}
I am dreading having to assign an int number to every character & making 20 different scripts. Thanks all for your help.
P.S. alternatively, if there is a way that I could check if any string in the array = name then I could make that work too. So something like:
if (unlocked[1-20] = name)
{
//execute unlocked code
}
Answer by NoseKills · Apr 21, 2015 at 02:56 PM
EDIT: I was on mobile so i wrote as little as possible.
With arrays you are stuck with integer indexing, but there are other types of lists & Dictionaries that make it possible to index stuff with other datatypes, like stings or even GameObjects etc.
Here's an example using a Dictionary.
using System.Collections.Generic;
...
Dictionary<string, bool> unlocked = new Dictionary<string, bool>();
unlocked.Add("something", false);
...
//this will throw an error if u didn't add anything with key "something"
bool isUnlocked = unlocked["something"];
//this is safer. Enters the if only if key "something" exists.
// and changes the value of isUnlocked to the found value
bool isUnlocked = false;
if (unlocked.TryGet("something", out isUnlocked))
{
if (isUnlocked)//do something...
Answer by ty-b97 · Apr 21, 2015 at 03:16 PM
Negative. Array's all have to be accessed via an index.
To the second question however, you could loop through the entire array to accomplish that.
Your answer
![](https://koobas.hobune.stream/wayback/20220613193633im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Converting a string to an int 2 Answers
C# String Array Has Missing or Incorrect Keycode Strings 2 Answers
Saving 30 bools in an array possible? 2 Answers
How do I make ALL other booleans in a bool array false when one is true ? 2 Answers
Can I create a list with an int/float and a string? C# 2 Answers