The list visibleItems is successfully filled, but when the TakeItem () is executed, the content is not in place. Although unity shows that the list is full.
Why is this happening and how can this be fixed?
public class Player : Creature { public List<GameObject> visibleItems; public List<GameObject> inventory; void OnTriggerEnter2D(Collider2D coll) { if (coll.gameObject.tag == "Item") { log.text = log.text + "\n " + "Ты видишь " + coll.gameObject.name + "!"; Debug.Log("Ты видишь " + coll.gameObject.name + "!"); visibleItems.Add(coll.gameObject);//объект исправно попадает в visibleItems } } void OnTriggerExit2D(Collider2D coll) { if (coll.gameObject.tag == "Item") { visibleItems.Remove(coll.gameObject);//объект на месте и успешно удаляется } } public void TakeItem()// при вызове метода, список visibleItems отображается в unity заполненным { Debug.Log("TakeItem"); foreach (GameObject o in visibleItems) //список оказывается пустым! { visibleItems.Remove(o); o.SetActive(false); inventory.Add(o); Debug.Log("Ты берешь " + o.gameObject.name); } } }