- Home /
 
 
               Question by 
               nyonge · Sep 13, 2015 at 07:18 PM · 
                c#unity 5dictionary  
              
 
              Destroying all GameObject values in a Dictionary
How do I destroy all the GameObject values (while leaving the Keys intact) in a dictionary, at once? C#
For example, if it were juts a List, I'd do
 while (myList.Count > 0) {
     Destroy(myList[0]);
     myList.RemoveAt(0);
 }
 
               But I'm having trouble with a dictionary where the keys are GameObjects (that I don't want to destroy) and the Values are gameobjects that I DO wanna destroy.
Thanks!
               Comment
              
 
               
              Not sure if I understand. Wouldn't a foreach loop work with this?
 foreach(var go in myList.values)
        Destroy(go);
                 Answer by Taylor-Libonati · Feb 28, 2017 at 10:41 PM
 //Destroy each gameObject but don't modify the dictionary
 foreach(KeyValuePair<string, GameObject> kvp in objects){
     Destroy(kvp.Value);
 }
 
 objects.Clear (); //To Clear out the dictionary
 
              Your answer