- Home /
How to return index of List element?
Hello, I am working on a game right now, and I'm kind of stuck.
It would take too much time to explain what I'm trying to do exactly, so I'll use fruits to simplify it.
Basically, I have manually populated a sprite list with the following fruits:
Element 0: Apple Element 1: Strawberry Element 2: Peach Element 3: Orange Element 4: Lemon Element 5: Banana
I am instantiating a prefab with an image component and a script on it. On start, I load these sprites randomly on the screen inside of a scrollview as buttons with the associated images.
Now, my question is, how could I get the index number of each button that I press? For example I press the button with an orange on it, how could I return 3? By the way these buttons are only present when I start the game.
Thanks in advance.
It would be helpful if you provided some code so we could give you an answer that will work better for you. This is because whatever I provide you will need to be modified to fit into your code. There are too many unknowns to give you an answer that will work for you.
Please read the FAQ for some helpful information on how better to create a detailed and specific question that will get you the best answers every time.
Answer by getyour411 · Dec 11, 2016 at 11:13 PM
Using System.Linq
int ListIDX = yourList.FindIndex(x => x.ElementName == "Apple");
(I think, untested; relace "ElementName" with your actual List element name for the attribute