How do I stop my list getting reset when I run my game?

Viewed 36

I have a class with a list of game objects in it set up like this:

public class ObjectList : MonoBehaviour {

public List<GameObject> listOfObjects;

I have another class creates objects and then adds them to the listOfObjects like this (this is a cut down version of the function):

public void InsertToList(Vector3Int myPosition)
    {
        GameObject g = GameObject.Find("Grid");
        ObjectList listScript = g.GetComponent<ObjectList>();
        GameObject tileObject = new GameObject("test");
        listScript.listOfObjects.Add(tileObject);
    }

I fill my listOfObjects in the editor but when I run the game it seems to wipe it and I'm not sure why. Heres a gif of what happens

I tried adding [SerializeField] above the listOfObjects but that didnt seem to solve it. I'm not really sure what I'm doing wrong.

0 Answers
Related