I’m trying to make a small game for myself to learn more about unity and in the game the player will have units (that has a class of its own) with randomized stats added to a List every minute.
A new unit will be added to the List every minute and the selected unit will be removed from the list to be assigned somewhere else.
I want to update the corresponding UI text panel when a new unit is added or a unit is removed. How can i achieve this.
Also I’m new to Unity so if this is a dumb question I’m really sorry.
Not a dumb question.
I’m guessing you already have an array with the units and some sort of way to add and remove. You can use something like events for this.
Example:
List list = new List() ;
public event Action onAdded;
public event Action onRemoved;
public void Add(Unit unit)
{
list.Add(unit);
onAdded?.Invoke(unit);
}
public void Remove(Unit unit)
{
if (list.Remove(unit))
onRemoved?.Invoke(unit);
}
Then you can in your UI code have a reference to this class and listen to events:
private void Start()
{
myArmyList.onAdded += OnUnitAdded;
}
private void OnUnitAdded(Unit unit)
{
Refresh...
}