[Help] Getting variable based on string value.

Hello, so I am doing something which is becoming quite complex, and would like to know if someone knows how to do this, or what exactly I should do.

    public static bool[] boolHolder;
    public static bool yellow;
    public static bool flamingo;
    public static bool parrot;
    public static bool pelican;
    public static bool vulture;

I have the following bools. First I would like to know If I can put those static bools inside the array.
Then what I want is:

    public static void MakeBoolTrue(string name)
    {
        //This is an idea of how it would work, however I am not sure if it would actually work.
        //For each bool I have inside my boolHolder, which would be: yellow, flamingo, parrot,
       //pelican and vulture, I check if this bool name is equal to the name string. 
       //If it is and the bool value is false, I make this bool true

        foreach (bool boolean in boolHolder)
        {
            if(boolean.name == name && boolean == false)
            {
                 boolean = true;
            }
        }
    }

Does anyone have any idea or different approach to make this logic. Baiscally, I am doing this to unlock a player and make it available to use. Then to know, I will probably just instead of making a void making a public static bool, and returning the bool value, by using a similar logic.

Thanks for the help!
Fritz

You should probably use a Dictionary or a List that contains a custom class. But let’s go with Dictionary in this case. Recommended viewing beforehand: Lists and Dictionaries - Unity Learn

so you could declare a:

Dictionary<string, bool> boolHolder = new Dictionary<string, bool>();

in your code. Now that means you don’t have to create a ton of other bool variables, you just have to initialize some dictionary entries like:

boolHolder["yellow"] = false;
boolHolder["flamingo"] = false;

and so on and so forth.

Then, your MakeBoolTrue function would simply look like:

public void MakeBoolTrue(string name){
   boolHolder[name] = true;
}

Just make sure that you add some checks in beforehand to make sure the name exists within the dictionary or else you’ll get some exceptions.

Okay I got a problem, and hope you can help :smiley: @Anathaerine

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public class newText : MonoBehaviour {

 public static Dictionary<string, bool> playerHolder = new Dictionary<string, bool>();

 public void OnEnable()
 {
     if(playerHolder.Count == 0)
     {
         playerHolder.Add("yellow", true);
         playerHolder.Add("flamingo", true);
         playerHolder.Add("parrot", true);
         playerHolder.Add("pelican", true);
         playerHolder.Add("vulture", true);
     }
     Load();
 }
 public void OnDisable()
 {
     Save();
 }

 public static void EnablePlayer(string name)
 {
     playerHolder[name] = true;
 }

 //Error is here...
 public static bool hasPlayer(string name)
 {
     return playerHolder[name];
 }

 public void Save()
 {
     BinaryFormatter bf = new BinaryFormatter();
     FileStream file = File.Create(Application.persistentDataPath + "test.dat");
     Data data = new Data();
     data.playerSave = playerHolder;
     bf.Serialize(file, data);
     file.Close();
 }
 public void Load()
 {
     if (File.Exists(Application.persistentDataPath + "test.dat"))
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Open(Application.persistentDataPath + "test.dat", FileMode.Open);
         Data data = (Data)bf.Deserialize(file);
         file.Close();
         playerHolder = data.playerSave;
     }
 }

}

[Serializable]
class TestDataSave
{
public Dictionary<string, bool> playerSave;
}

It gives me an error where I commented, saying Object Reference not set to an instance of an object.