Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

List of lists in Inspector

Discussion in 'Editor & General Support' started by valter-home, Jan 10, 2018.

  1. valter-home

    valter-home

    Joined:
    Sep 22, 2015
    Posts:
    73
    Hi all,
    I have really searched everywhere but without finding answers.
    I have to show in inspector a Vector3 list of lists starting from a MonoBehaviour.
    I tried adding classes and anyway all the examples found but without succeeding.
    I tried
    Code (CSharp):
    1. public class Point{
    2.      public List<Vector3> list;
    3. }
    and

    Code (CSharp):
    1. public List<Point> myPoints= new List<List<Point>>();
    as found in an example but it raises me error.
    Can someone give me a practical example? I do not know what to do anymore.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,991
    Thats different type, List<Point> vs List<List<Point>>

    this seems to work,
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Lister : MonoBehaviour
    6. {
    7.     public PointList ListOfPointLists = new PointList();
    8. }
    9.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6. public class Point
    7. {
    8.     public List<Vector3> list;
    9. }
    10.  
    11. [System.Serializable]
    12. public class PointList
    13. {
    14.     public List<Point> list;
    15. }
    16.  
    upload_2018-1-11_12-2-3.png
     
  3. valter-home

    valter-home

    Joined:
    Sep 22, 2015
    Posts:
    73
    Thank you very much, it works!
    But how should I address the lists?
    With
    Code (CSharp):
    1. ListOfPointLists.list [0].list.Add (new Vector3 (1, 1, 1));
    I can insert new Vector3 but must I first initialize a new list of Points?
    Currently, the List Size does not change in the Inspector.
     
  4. valter-home

    valter-home

    Joined:
    Sep 22, 2015
    Posts:
    73
    Yes, I need to add
    Code (CSharp):
    1. ListOfPointLists.list.Add(new Point());
     
  5. valter-home

    valter-home

    Joined:
    Sep 22, 2015
    Posts:
    73
    But I still have problems!
    If I write
    Code (CSharp):
    1. ListOfPointLists.list.Add(new Point());
    2. ListOfPointLists.list [0].list.Add (new Vector3 (1, 1, 1));
    the inspector shows a new list but:
    ListOfPointLists.list [0] .list.Add (new Vector3 (1, 1, 1))
    returns a reference type not set error if the number of lists in Inspector starts with zero.
    The list increases by one but raises the error I said.
    If I start with the number of lists set at least one works regularly.
    What am I forgetting?
     
  6. valter-home

    valter-home

    Joined:
    Sep 22, 2015
    Posts:
    73
    Ok, I think I finally found the solution:
    Code (CSharp):
    1. ListOfPointLists.list.Add(new Point());
    2. int index = ListOfPointLists.list.Count - 1;
    3. ListOfPointLists.list[index].list = new List<Vector3> ();
    4. ListOfPointLists.list[index].list.Add (new Vector3(1,1,1));
    This way the list is initialized and accepts the input of the Vector3 value. No errors and the inspector correctly shows the content.
    Thanks mgear for your support.
     
  7. mrsev

    mrsev

    Joined:
    Oct 1, 2016
    Posts:
    2
    I know this is very old but a better way for you to initialize this is to make a constructor inside your Point class :

    Code (CSharp):
    1. [System.Serializable]public class Point
    2. {  
    3.     public List<Vector3> list;
    4.     public Point()
    5.     {
    6.         list = new List<Vector3>();
    7.     }
    8. }
     
    ow3n, Lostshun, Griffo87 and 2 others like this.
  8. msganeshsharma

    msganeshsharma

    Joined:
    Dec 4, 2016
    Posts:
    4
    How i can remove and element from this type of list..?
     
  9. valter-home

    valter-home

    Joined:
    Sep 22, 2015
    Posts:
    73
    e.g. you could use
    Code (CSharp):
    1. ListOfPointLists.list.RemoveAt(*list index*)
    to remove a whole list
    or
    Code (CSharp):
    1. ListOfPointLists.list[*list index*].list.RemoveAt(*element position*)
    to remove an item from the list
     
  10. sevdigim1451

    sevdigim1451

    Joined:
    May 2, 2020
    Posts:
    40
    I did list of lists and I did it static hereby I can access everywhere it but I can't add a value it give null referance exception error. I equalized the array to new int or string or etc [count] but it didn't. Have you any solution about this? System.Serializable dont let static function is it true? How can I create list of list and access it from everywhere and save last data? Thanks!
     
  11. sevdigim1451

    sevdigim1451

    Joined:
    May 2, 2020
    Posts:
    40
    How can I do them static for access everywhere?
     
  12. matekk323_unity

    matekk323_unity

    Joined:
    Aug 25, 2021
    Posts:
    1
    To make something static you use "static" keyword. i.e.
    public static List<Point> list;

    And then to access it you would have to do it this way:
    PointList.list[0].Add(something);
     
  13. ryusdarling

    ryusdarling

    Joined:
    Oct 20, 2022
    Posts:
    2

    worked like a charm tysm <3