My list is overwriting the data it was holding with the newest element added to the list.

I’m not very good at coding so I’m sure this is just something dumb that I’m doing wrong or not doing, but basically I have a list its holding a class in it and it seems to be working the first time I use it. Then if I add to that list again it overwrites the data in the first element with the newest one. I’m not exactly sure what I need to do to fix it so if someone could take a look and let me know whats wrong with it that would be a big help.

I believe you can ignore everything in the NewUnitStats () Method I was using that for testing and for calling AddPlayerToList () method but, I might be wrong.

public class MakeNewUnit : MonoBehaviour {

    private CreateNewPlayerCharacter createNewPlayerCharacter;

    public List<CreateNewPlayerCharacter> listOfPlayerCharacters;

  

    public string unitName;
    public string unitClassName;
    public string unitRaceName;

    public ScriptableObjectTest[] scriptObjTester;
    
    void Start()
    {
        createNewPlayerCharacter = GetComponent<CreateNewPlayerCharacter>();
    }
    public void NewUnitStats()
 {
        unitName = createNewPlayerCharacter.baseUnitStats.unitName;
        unitClassName = createNewPlayerCharacter.pickedClass.className;
        unitRaceName =createNewPlayerCharacter.pickedRace.raceName;

        Debug.Log("click working " + unitRaceName);

        scriptObjTester = createNewPlayerCharacter.baseUnitStats.scriptObjSaveTest;
        
        foreach (ScriptableObjectTest scriptObjTester in scriptObjTester)
        {
            int counterValue = scriptObjTester.value;
            counterValue += 25;
            //counterValue = counterValue + 20;
            //Debug.Log(scriptObjTester.name);
            //Debug.Log(scriptObjTester.value);
            //Debug.Log(counterValue);
        }
        

        AddPlayerToList();
    }

    private void AddPlayerToList()
    {
        listOfPlayerCharacters.Add(createNewPlayerCharacter);
        int index = listOfPlayerCharacters.IndexOf(createNewPlayerCharacter); // finds the number in the list
        foreach (CreateNewPlayerCharacter unit in listOfPlayerCharacters)
        {
            Debug.Log("Unit " + index + " " + unitClassName + " " + unitRaceName);
        }
    }
   
}

I think I know what the problem here is.

The problem is that each of the CreateNewPlayerCharacter components in your list references the same component that’s on your game object (the one which this MakeNewUnit component is on).

All the items in your list are just references to the main component (in this case, the component that you have attached to the game object) and they are not separate instances of that component.

So if you change something on the main component, when you go and print the values for each item, they all “look” at the main component and print the main component’s values instead.

You have 3 choices (that I could think of) if you want to do what I think you want to do (which is to save multiple instances of the same component, right?)

  1. Use scriptable objects. There are lots of tutorial about this on Youtube, and they explain it better there. But basically they’re used to store data (like the different values on your component you want to store) in separate .asset files. However, this is useless when you want to procedurally create them during runtime. But if you’re creating premade units (that is, you don’t procedurally create them) then that’s perfect to use. You can then copy their values to components when you create the new, different components using AddComponent()

  2. Attach different instances of the component with different values using AddComponent(). You can attach all of the new instances in the same object, or on different objects; that is up to you.

  3. You can serialize (or save) the values to files and load them when you need them. You can save them in .json files using this, for example: Releases · SaladLab/Json.Net.Unity3D · GitHub.
    If you decide to use this, the documentation is here: Introduction

Hope this helped you out, even for a bit! If you still have more questions, please don’t hesitate to ask! Happy coding!

Your list is fine and gets updated just right, its how you choose to display the data that’s wrong. Try the following:

	private void AddPlayerToList()
    {
		// This is fine
        listOfPlayerCharacters.Add(createNewPlayerCharacter);
        // finds the number in the list
        foreach (CreateNewPlayerCharacter unit in listOfPlayerCharacters)
        {
			// Find the Index
			int index = listOfPlayerCharacters.IndexOf(unit);
			 
			// Assign the names
			string className = unit.unitClassName;
			string raceName = unit.unitRaceName;
			
            Debug.Log("Unit " + index + " " + className + " " + raceName);
        }
    }