Adding to a seriazable list in Unity 2021

I am trying to serialize a list of integers to save and am getting an “object reference not set to an instance of an object” error - this is for a code that I had used in Unity 2018 and am trying to use in Unity 2021.1.19. When I save individual integers, floats, etc to “unitSave” in the code below, there are no problems - it’s only when I try to add them to a list.

public class ObjectSaveStats
{
    public List<float> playerUnits;
    public string unitName;
}

public void Save()
{
    List<ObjectSaveStats> saveList = new List<ObjectSaveStats>();
       for (int i = 0; i < allUnitsList.Count; i++)
       {
unitSave.unitName = allUnitsList*.GetComponent().unitName;  THIS WORKS
unitSave.playerUnits.Add(allUnitsList*.GetComponent().unitID;  THIS GENERATES AN ERROR
       }
}

Your list in *ObjectSaveStats * is not correctly defined. Plus, there’s a missing bracket at the end of your Add line.

Edit: Also @mjonasz are unitName and unitID components OR variables in a different component?

Solution: the list had to be explicity initialized:

 public List<float> playerUnits = new();