How to get the position of an object in a list

I’m making a TBS where you control a bunch of pieces and have to destroy the enemy ones with your weapons, and in that, I have a list with a bunch of gameObjects (the pieces that you control) and I use their position in this list to make the save function work like this:

And so, I need a variable that says where the piece is in the list. I’m actually using this “position” int as a public one and setting it manually in each scene, but doing so is a pain in the ass and I wanted to know if there’s some way to get the numeric value of the object position in a list.

I think you’ll find it fairly easy, but I didn’t found how to do it anywhere and you should have noticed that I’m not very good on programming and english too, but any help is welcome.

OBS: I didn’t thought that any other of my scripts would be necessary, but if some of them is, I’ll be glad to send it.

1 Like

Use List.IndexOf to find what index the gameObject is in your list.

int position = myList.IndexOf(gameObject1);
11 Likes

Really thanks dude, it worked exactly as I needed.