• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Sep 16, 2015 at 11:08 AM by Immortus12 for the following reason:

The question is answered, right answer was accepted

avatar image
Question by Immortus12 · Sep 07, 2015 at 03:46 AM · script.listitems

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)`

Comment
petervaz
AibNihan

People who like this

2 Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MaT227 · Sep 07, 2015 at 06:20 AM 0
Share

I suggest you to post some code and maybe a small screenshot of the inspector, this might help solve your issue.

avatar image Immortus12 · Sep 07, 2015 at 09:07 AM 0
Share

For now the only code related to that list is:

public List tableRowPrefabs = new List ();

That's mmy inspector:

lista.png (13.4 kB)
avatar image CodeMasterMike · Sep 07, 2015 at 02:11 PM 0
Share

What kind of objects do you put into the list? Is it GameObjects or some custom class?

avatar image Immortus12 · Sep 07, 2015 at 02:44 PM 0
Share

Prefabs, its just bunch of ui elements in UI Panel.

tablerow.png (4.2 kB)

2 Replies

  • Sort: 
avatar image
Best Answer

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.

Comment
Immortus12
petervaz
AibNihan
Blitzkriegmlg
goldcloud144

People who like this

5 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Immortus12 · Sep 16, 2015 at 11:00 AM 0
Share

Thank you, I use SerializeField and it works now. Thanks again.

avatar image AibNihan · Jan 31, 2018 at 08:04 PM 0
Share

Thanks! It's Really Helpful Thank you again.

avatar image

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;

Comment
Immortus12

People who like this

1 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Immortus12 · Sep 07, 2015 at 01:51 PM 0
Share

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".

avatar image Suddoha · Sep 07, 2015 at 02:54 PM 0
Share

@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.

avatar image Immortus12 · Sep 08, 2015 at 02:03 AM 0
Share

This is my script which has a method CreateLeague. That method fill "tableTeam" list and call method "ShowTable" from Table Class.

 public class MyClub : MonoBehaviour
 {
         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 : MonoBehaviour
 {
     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);
         }
     }
 }
 
avatar image sys12 Immortus12 · Sep 16, 2015 at 10:49 AM 0
Share

Move the public List<GameObject> tableRowPrefabs; from Table to MyClub 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 MyClub. Or do everything which need tableRowPrefabs inside MyClub 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 MonoBehaviour. Table class doesn't have to a MonoBehavior if you move the tableRowPrefabs to MyClub.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Best item list 1 Answer

stack by quanity...? C# 2 Answers

Find index of item in list 1 Answer

How do I make it so that this scripts adds to a list without overfilling it. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges