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

Question Find Item in list by Index?

Discussion in 'Scripting' started by give_me_pen, Jul 29, 2023.

  1. give_me_pen

    give_me_pen

    Joined:
    Oct 18, 2021
    Posts:
    24
    Is there a way to reference an item in a list by its index? If there is I cannot find it, if there isn't, how come?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Perhaps you mean you just want to access list items by using an index?

    Code (CSharp):
    1.         List<int> blah=new List<int>();
    2.         blah.Add(123);
    3.         blah.Add(312);
    4.         blah.Add(245);
    5.  
    6.         Debug.Log(blah[0]); // prints 123
    7.         Debug.Log(blah[1]); // prints 312
    8.         Debug.Log(blah[2]); // prints 245
    9.  
     
  4. give_me_pen

    give_me_pen

    Joined:
    Oct 18, 2021
    Posts:
    24
    Oh my gosh I feel so stupid, this is what no sleep does to you

    for some reason I kept trying to use a method like " list1.Index(1) ". I was thinking "I know it's been a while since I've done any coding, but I was sure you could do something like this"

    thank you guys for taking the time to reply to such a foolish question
     
  5. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    No worries. I'm an old school array fan myself and so I often forget how to use lists. :)