Why my public list, filled with items, is in play mode empty?

Hi, I have public list of Game Objects and im filling it with my Game Objects before launching but when i try to access to that list in play mode it seems to be empty. When i hit pause and look at my list it has all items so i dont know why according to my script list is empty…

I check it by that lineprint (LIST.Count)

Your problem here is this line:

Table tableScript = new Table();

Since Table is a class derived from MonoBehaviour you can’t create an instance with “new”. Also it seems you have already an instance somewhere in the scene where you setup your GameObject List. So you most likely want to use that instance instead of creating a new one.

The most common solution is to make your tableScript variable public or use the SerializeField attribute on it. That way the variable will show up in the inspector. Now you can simply drag and drop your Table instance to the variable:

public Table tableScript;

// or
[SerializeField]
Table tableScript;

In Unity if you have an array or List of a serializable type, so that it will show up in the inspector, you don’t need to create an instance of that array / List as this is done by the inspector automatically. The values are serialized along with the instance of the class.

However this only works when you attached the script to an object within the editor. If you create components at runtime (by using AddComponent<ClassName>()) those variables won’t be initialized automatically. Of course if you create a new instance with AddComponent, that instance can’t have any serialized values, so all variables will have their default values.

public List tableRowPrefabs = new List ();

This will empty your list. Remove everything after ‘=’.

Should be only
public List tableRowPrefabs;