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?!?!

Discussion in 'Scripting' started by mohsenz, Feb 16, 2021.

  1. mohsenz

    mohsenz

    Joined:
    Aug 10, 2016
    Posts:
    29
    I don't know if I choose the best plan,
    I have a list of objects, some of them have the same name, I wanna connect the repeated ones with lines, so I plan to first put them in their own list!
    so I'm challenging with something like this:

    Code (CSharp):
    1.  
    2.     public List<GameObject> Similars = new List<GameObject>();
    3.        for (int i = 0; i < ObjectsList.Count; i++)
    4.         {
    5.             if (ObjectsList.Contains(ObjectsList[i]))
    6.             {
    7.                 /// it just put the first similar group and I dont know how to create a new list for the next group I also can't manage to create a list of List so then with a counter start filling the next List in the list!!!
    8.                 Similars = ObjectsList.FindAll(s => s.name.Equals(ObjectsList[i].name));
    9.             }
    10.         }
    any solution or smarter way?!
    thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    The above is just a list of GameObjects.

    If you want a list of lists of GameObjects, it would look like this:

    Code (csharp):
    1. public List<GameObject> Similars = new List<GameObject>();
    To use it you need to create it, but you also need to create its interior lists as you go along:

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Lists : MonoBehaviour
    6. {
    7.     public List<List<GameObject>> ListOfLists = new List<List<GameObject>>();
    8.  
    9.     void Start ()
    10.     {
    11.         ListOfLists = new List<List<GameObject>>();
    12.  
    13.         for (int listNumber = 0; listNumber < 3; listNumber++)
    14.         {
    15.             // gotta make the inner list so we can add to that
    16.             ListOfLists.Add( new List<GameObject>());
    17.  
    18.             for (int itemNumber = 0; itemNumber < 5; itemNumber++)
    19.             {
    20.                 var oneItem = new GameObject(
    21.                     "Item: listNumber:" + listNumber + ", itemNumber: " + itemNumber);
    22.  
    23.                 ListOfLists[listNumber].Add( oneItem);
    24.             }
    25.         }
    26.     }
    27. }
     
  3. mohsenz

    mohsenz

    Joined:
    Aug 10, 2016
    Posts:
    29
    Thank you so much, the solution works perfectly! the next step is more confusing to me.

    I have two list of lists: Objects and lines with the same quantity, Now, I'm trying to connect lines to their objects,
    listoflist.jpg
    my first try was about storage all the Vector3s in a List and set them to all objects, but the problem is that it creates Vector3s Uninterrupted so the lines are always moving between objects!

    Code (CSharp):
    1.     foreach (List<GameObject> l in ListOfLists)
    2.         {
    3.             for (int i = 0; i < l.Count; i++)
    4.             {
    5.                 Vector3 p = l[i].transform.position;
    6.                 AllVectorsList.Add(p);
    7.             }
    8.         }
    9.         Debug.Log("ALL Transforms:  " + AllVectorsList.Count);
    10.    
    11.  
    12.         foreach (List<LineRenderer> l in LineConnections)
    13.         {
    14.             for (int i = 0; i < l.Count; i++)
    15.             {
    16.  
    17.                 if(_counter < AllVectorsList.Count - 1)
    18.                 {
    19.                     l[i].SetPosition(0, AllVectorsList[_counter]);
    20.                     l[i].SetPosition(1, AllVectorsList[_counter + 1]);
    21.  
    22.                     l[i].SetWidth(0.07f, 0.07f);
    23.  
    24.                     _counter++;                
    25.                 }
    26.                 else
    27.                 {
    28.                     _counter = 0;
    29.                 }
    30.  
    31.             }
    32.         }
    33.  
    I also try using Zip two List's but couldn't get a result yet!
    any solution?!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    I think you need multiple line renderers to do multiple lines, if that's what you are saying.
     
  5. mohsenz

    mohsenz

    Joined:
    Aug 10, 2016
    Posts:
    29
    this code is in the update, so It adds multiple lines in each frame. the current problem!
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Strip it down to just 4 points and 2 line segments.

    Are you setting the line point count right?

    Why are two lines still connected then?

    etc. etc. etc, basically debugging 101.

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
    MoonJellyGames likes this.