- Home /
The question is answered, right answer was accepted
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 line`print (LIST.Count)`
I suggest you to post some code and maybe a small screenshot of the inspector, this might help solve your issue.
For now the only code related to that list is:
public List tableRowPrefabs = new List ();
That's mmy inspector:
What kind of objects do you put into the list? Is it GameObjects or some custom class?
Answer by Bunny83 · Sep 16, 2015 at 09:55 AM
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.
Thank you, I use SerializeField and it works now. Thanks again.
Answer by Raresh · Sep 07, 2015 at 09:13 AM
public List tableRowPrefabs = new List ();
This will empty your list. Remove everything after '='.
Should be only public List tableRowPrefabs;
I did this but now when i try to get to items from that list , i get error "nullreferenceexception object reference not set to an instance of an object".
@Immortus12 You have to provide more code if you encounter errors as we cannot know what you're trying to do when the error occurs.
This is my script which has a method CreateLeague. That method fill "tableTeam" list and call method "ShowTable" from Table Class.
public class $$anonymous$$yClub : $$anonymous$$onoBehaviour
{
Table tableScript = new Table();
public void CreateLeague()
{
for (int i = 0; i < 20; i++)
{
tableScript.tableTeams.Add(new Team(database.leagueDB[0].Teams[i].Name, 0, 0, 0, 0, 0, 0));
}
tableScript.ShowTable ();
}
}
And this is my class "Table":
public class Table : $$anonymous$$onoBehaviour
{
public List<GameObject> tableRowPrefabs;
public List<Team> tableTeams = new List<Team> ();
public void ShowTable()
{
for (int i = 0; i < tableTeams.Count; i++)
{
print (tableRowPrefabs[0].name);
}
}
}
$$anonymous$$ove the public List<GameObject> tableRowPrefabs;
from Table to $$anonymous$$yClub and assign the list in editor. If the table class really wants the tableRowPrefabs, pass the reference of tableRowPrefabs to the Table class created in $$anonymous$$yClub. Or do everything which need tableRowPrefabs inside $$anonymous$$yClub if it is not necessary for the Table class to have this field.
I think your design might be incorrect. It probably will cause trouble if you are trying to create an instance of a $$anonymous$$onoBehaviour. Table class doesn't have to a $$anonymous$$onoBehavior if you move the tableRowPrefabs to $$anonymous$$yClub.