Problem with if statement using kpv/dictonary in a foreach loop

So i’m having a problem with using a if statement using a kpv/dictonary in a foreach loop, it looks something like this.

public Dictionary<string, int> conditions =  new Dictionary<string, int>();

foreach(var condition in conditions.ToList())
        {
                  Debug.Log(condition);
            if(conditions[condition.Key] == 0)
            {
                Debug.Log(condition);
                switch(condition.Key)
                {
                    case "Stun":
                        stunObj.SetActive(false);
                        break;
                    //etc for each
                }
            }
            else
            {
                switch(condition.Key)
                {
                    case "Stun":
                        stunObj.SetActive(true);
                        stunNum.SetText(condition.Value.ToString());
                        break;
                    //etc for each
                }
            }
     }

The thing is it only happens when the value is 1, on 2 (and above) and on 0 it works fine, but when the variable is on 1 it disappears, i know its working fine because of the debug.logs printing the number being 1 so something is wrong with the if statement. Any help?

Right off the bat , i can tell you’re using the dictionary very incorrectly , i dunno if you wrote this code or just kept adding edits until it compiles , but you need to really watch tutorials/articles and understand how collections work , here are my remarks

  • foreach(var condition in conditions.ToList()) : you’re not iterating on the dictionary here, you’re converting it to a list first THEN iterating on the list , there’s no use for that here , just do foreach(var condition in conditions) directly.
  • if(conditions[condition.Key] == 0) you’re redoing the lookup in the dictionary when it’s already there given to you , just use if(condition.Value == 0)

Now with that out of the way , what do you want to do here ? if understood correctly , you want to toggle a gameobject based on if there’s a “Stun” or not.
This can be expressed much easier like this

public Dictionary<string, int> conditions =  new Dictionary<string, int>();

bool shouldStun = conditions.TryGetValue("Stun" , out int value) && value != 0;

if(!shouldStun)
{
     stunObj.SetActive(false);
}
else
{
     stunObj.SetActive(true);
     stunNum.SetText(value.ToString());
}