public void CloseMenu(AMenu m) { m.SetClosed(); // check if it's the top pop-up if (popups.Size() > 0 && popups.First() == m) { Destroy(popups.First().gameObject); popups.RemoveFirst(); if (popups.Size() > 0) { popups.First().gameObject.SetActive(true); } } var it = popups.Iterator(); // remove menu from the list while (it.Next()) { if (it.Value() == m) { Destroy(it.Value().gameObject); it.Remove(); } } it = menus.Iterator(); // remove menu from the list while (it.Next()) { if (it.Value() == m) { Destroy(it.Value().gameObject); it.Remove(); } } }