Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
1
Question by DJICS · Jun 17, 2015 at 03:20 AM · generic listmultidimensional

see C# 2D Generic List in inspector

Hi everybody,

I've got a "containerList" that have 10 Lists with 45 items in it. The items are made from this class (which is serialized) :

 using System;
 using UnityEngine;
 using System.Collections;
 [Serializable]
 
 public class SaveTalentClass {
 
     [SerializeField]
     public int talId;
     public int talCurRank;
     public int talMaxRank;
 
     public SaveTalentClass(int id, int cRank, int mRank)
     {
         talId = id;
         talCurRank = cRank;
         talMaxRank = mRank;
     }
 
     public SaveTalentClass()
     {
 
     }
 }

And I initialize my lists through a for loop like this :

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 using System.Linq;
 [System.Serializable]
 
 public class PlayerData : MonoBehaviour {
     
     [SerializeField]
     public List<List<SaveTalentClass>> containerList = new List<List<SaveTalentClass>>();
 
     void Start ()
     {
         for(int i = 0; i < 10; i++)
         {
             containerList.Add (new List<SaveTalentClass>());
                 for(int j = 0; j < 45; j++)
                 {
                     containerList[i].Add (new SaveTalentClass(j,0,0));
                 }
         }
     }

But I can't see my "containerList" in the inspector... I found this thread on a similar issue http://answers.unity3d.com/questions/289692/serialize-nested-lists.html but wasn't able to adapt my script to make it work.

Can anyone be kind enough with a beginner and explain me how can I do ?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Shark-Boy · Jun 17, 2015 at 04:35 PM

So I had the same problem a little while ago and it took quit the search to find a solution. But I did get a working solution here it is.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class GameController : MonoBehaviour 
 {
     public List<ListableClass> containerList = new List<ListableClass>();
 }
 
 [System.Serializable]
 public class ListableClass
 {
     public List<SaveTalentClass> innerList = new List<SaveTalentClass>();
 }

I did use a array insted of a list in my code so if it still wil not work you could try that but it should work fine the way it is.

Comment
Add comment · Show 18 · 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 DJICS · Jun 17, 2015 at 07:49 PM 0
Share

Thank you for the tips but unfortunately it didn't work. It seems that your solution isn't using a multidimensionnal List like I do for holding the 10 initialized List in it.

$$anonymous$$aybe you made a type error in your provided code, Or (another highly probable possibility) I'm really a beginner and wasn't able to implement it right ;)

avatar image Shark-Boy · Jun 17, 2015 at 08:21 PM 0
Share

With my script added to it what is your code now?

avatar image DJICS · Jun 18, 2015 at 08:32 AM 0
Share

In fact I didn't understood the concept behind that class List. When do I have to use the "innerList" ? During initialization in the for loop ?

I tried this (I commented stuff that we don't need yet):

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 using System.Linq;
 
 public class PlayerData : $$anonymous$$onoBehaviour {
     
     int activeTree;
 //    Talent$$anonymous$$anager manager;
     TalentDatabase database;
     
 
     public List<List<ListableClass>> containerList = new List<List<ListableClass>>();
 
     [System.Serializable]
     public class ListableClass
     {
         public List<SaveTalentClass> innerList = new List<SaveTalentClass>();
     }
 
     void Start ()
     {
 //        manager = GameObject.Find ("Talent$$anonymous$$anager").GetComponent<Talent$$anonymous$$anager>();
         database = GameObject.Find ("TalentDatabase").GetComponent<TalentDatabase>();
         
 /*        if(File.Exists(Application.persistentDataPath + "/talentList.dat"))
         {
             var b = new BinaryFormatter();
             var f = File.Open(Application.persistentDataPath + "/talentList.dat", File$$anonymous$$ode.Open);
             containerList = (List<ListableClass>)b.Deserialize(f);
             f.Close();
         }
         else
         {
         */    for(int i = 0; i < 10; i++)
             {
                 containerList.Add (new List<ListableClass>());
                 for(int j = 0; j < 45; j++)
                 {
                     containerList[i].Add (new SaveTalentClass(j,0,database.talents[j].tal$$anonymous$$axRank));
                 }
             }
     //        SaveLists();
         //}
     }
     
 /*    public void SaveLists ()
     {
         var b = new BinaryFormatter();
         var f = File.Create(Application.persistentDataPath + "/talentList.dat");
         b.Serialize(f, containerList);
         f.Close();
     }
     
     public void ResetLists ()
     {
         GameObject.Find ("TalentsPanel").Broadcast$$anonymous$$essage("ResetRank");
     }
     
     void Update ()
     {
         activeTree = manager.activeTree;
         if(Input.Get$$anonymous$$eyUp("space"))
         {
             Test();
         }
     }
     
     void Test()
     {
         print (containerList[activeTree][0].talCurRank);
     }*/
 }

But as expected, it didn't work, errors are :

 error CS1502: The best overloaded method match for `System.Collections.Generic.List.Add(PlayerData.ListableClass)' has some invalid arguments
 
 error CS1503: Argument `#1' cannot convert `SaveTalentClass' expression to type `PlayerData.ListableClass'

avatar image Shark-Boy · Jun 18, 2015 at 12:40 PM 0
Share

So the listable class must go outside of the public class PlayerData like this.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 // and so on
 
 public class PlayerData : $$anonymous$$onoBehaviour { // opening bracket for class
 
    int activeTree;
    // and so on
 
    // a list of the listableclass 
    public List<ListableClass> containerList = new List<ListableClass>();
 
    void Update() { // a function 
    
    }
 } //a closing bracket for the class
 
 // a new class
 [System.Serializable]
 public class ListableClass { //a class with a list in it
    public List<SaveTalentClass> innerList = new List<SaveTalentClass>();
 }

the listableclass has a list in it so if you make a list of listableclass's (like the one above) then you have a list of lists that will show up in the unity editor. So that should work now :)

avatar image DJICS · Jun 18, 2015 at 03:25 PM 0
Share

Thank you for helping me but it still doesn't work. Am I suppose to run the for loop to create the 10 Lists into the InnerList in a Start function inside the ListableClass ?

That really puzzle me... Once again, thank you for helping me and sorry for my lack of knowledges :(

Show more comments
avatar image
0

Answer by florinel2102 · Apr 21 at 10:27 PM

     [System.Serializable]
     public class SimpleList<T> where T: class
     {
         public List<T> myList;
     }
 

After you import the class in your project , you can simply reuse it whenever you want by List<SimpleList<SaveTalentClass>> or SimpleList<SaveTalentClass>[]

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multidimensional PlayerPrefs? 1 Answer

Generic List.Count always gives 0 2 Answers

Unity Editor Memory Spike Generic List 0 Answers

Push Custom Class to Array (JavaScript) 1 Answer

convert string to list of lists 1 Answer


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