Search Unity

accessing list of lists elements by index

Discussion in 'Getting Started' started by killerKeks26601, Apr 23, 2021.

  1. killerKeks26601

    killerKeks26601

    Joined:
    Jan 13, 2021
    Posts:
    31
    Hello,
    I have a question about lists of lists.
    How exactly do i reference an element at a certain index.
    Code (CSharp):
    1. for(int i = 0; i < allLists.Count; i++)
    2.         {
    3.             for(int x = 0; i < allLists[i].Count; x++)
    4.             {
    5.                 if(allLists[i][x].message == message)
    6.                 {
    7.                     path.Add(i);
    8.                     path.Add(x);
    9.                 }
    10.             }
    11.         }
    I wouldve thought it would look something like this.
    But its throwing me an ArgumentOutOfRangeException.
    I did some searching on StackOverflow but the solutions there dont seem to apply to my situation or maybe I didnt find the right solution.
    Can someone maybe help me out and show me how to access this the right way.
    Note that I need to keep track of at which index the searched item is at.
     
  2. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    You have a typo on line 3. It should be:
    Code (CSharp):
    1.  for(int x = 0; x < allLists[i].Count; x++)
     
    killerKeks26601 and Schneider21 like this.
  3. killerKeks26601

    killerKeks26601

    Joined:
    Jan 13, 2021
    Posts:
    31
    Thank you,
    hahaha S*** happens ¯\_(ツ)_/¯