Dictionary not changing the value

EDIT: After further research this seems to not be the best way of doing this. I have a space shooter with multiple weapons that are turned on and off with bool values. User selects what they want and that info is stored in the Player Prefs.

PlayerPrefs.SetString("Upgrade1", "Blaster");
PlayerPrefs.SetString("Upgrade2", "none");
PlayerPrefs.SetString("Upgrade3", "none");

I have create a Dictionary that holds the bool variables.

private bool blaster_Active, targeting_Active, missles_Active, laser_Active;

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

weaponsActivateBoolDict.Add("Blaster", blaster_Active);
weaponsActivateBoolDict.Add("LaserBeam", laser_Active);
weaponsActivateBoolDict.Add("Targeting", targeting_Active);
weaponsActivateBoolDict.Add("Missles", missles_Active);

In my Start() I call this method to activate the weapons the player has picked. When I debug and set break points I can see that everything is working as expected. But when I check the actual variable in the global area like the blaster_Active, it is still set to false, even though it showed as true in the method?

private void ActivateWeapons()
    {
        string[] PlayerPrefsWeaponsArray = new string[] { "Upgrade1", "Upgrade2", "Upgrade3", "Upgrade4", "Upgrade5" };
       
        foreach(string weapon in PlayerPrefsWeaponsArray) 
        {
            string pItem = PlayerPrefs.GetString(weapon); 
            if(pItem == "none")
                continue;
            else if (weaponsActivateBoolDict.ContainsKey(pItem))
                weaponsActivateBoolDict[pItem] = true;
        }
    }

I know I am doing something wrong but I am wondering if the value that I am storing in the Dictionary is not a reference to that bool variable in the global section? Thanks!

The bool type is a struct, which means when you use it as a parameter or input to a function (e.g. the Add() function on the dictionary class), it makes a copy of the variable, and doesn’t reference/modify the original bool variable. That means the dictionary stores a copy of the bool variable, and not the actual reference.


If you want to pass the reference, use a reference type as a container, e.g. a class, containing the bool variable:

class Bool
{
     public bool Value;
}

class MyScript : MonoBehaviour
{
     private Bool blaster_Active = new Bool(), laser_Active = new Bool();

     public Dictionary<string, Bool> weaponsActivateBoolDict = new Dictionary<string, Bool>();

     void Awake()
     {
          weaponsActivateBoolDict.Add("Blaster", blaster_Active);
          weaponsActivateBoolDict.Add("LaserBeam", laser_Active);
     }
}

(Now you can access the reference to the bool variable and edit it properly, like a reference type).


However, you may need to re-think what you’re doing instead. I have no idea why you don’t simply have a Weapon class containing a bool (for each weapon), to say whether it’s active or not. @larswik

I thought that is what it was, a copy and not a reference to it. Thanks for the explanation. I will look in to a Weapon Class.