// Destroyes previous children and re-creates them void DisplayLists() { UIPlayerResourceItem[] children = collectionArea.GetComponentsInChildren <UIPlayerResourceItem>(); // Destroy each item. Make sure to destroy the game object, otherwise just the component will be removed. foreach (UIPlayerResourceItem child in children) { Destroy(child.gameObject); } // Create all the objects for (int i = 0; i < playerResources.Count; ++i) { PlayerResourceEntry entry = playerResources[i]; UIPlayerResourceItem display = Instantiate(referenceObject, collectionArea.transform, false); display.DisplayObject = entry; display.buttonPlus.onClick.AddListener(() => UpdateResouceEntry(entry, 1)); display.buttonMinus.onClick.AddListener(() => UpdateResouceEntry(entry, -1)); display.gameObject.SetActive(true); } }
// Callback function void UpdateResouceEntry(PlayerResourceEntry entry, int changeAmount) { entry.value += changeAmount; entry.OnValueChanged?.Invoke(); }