- Home /
Delete first object of the same type in a list? C#
Hey guys! I have a list that stores gameobjects in a scene. I want to figure out how to find if the same object is in the list, and then delete the first one. To do the first step I used list.contains if (myList.Contains (object)) { //delete first gameobject in the list }
but I dont know how I would do the second step. Any help would appreciated!
Comment
Best Answer
Answer by JedBeryll · Dec 05, 2017 at 05:25 AM
Use IndexOf instead of Contains. Returns index if the list contains the object, -1 otherwise. Then you can do
if (myList.IndexOf(object) >= 0) {
myList.RemoveAt(index);
}