Is adding and removing elements of a list at runtime (in Update method) expensive?

Basically I have list that takes in a few gameobjects.

On Update, if certain conditions are met, the list should remove some of the gameobjects, or add some.

Is this expensive? Keep in mind that I am NOT CREATING game objects on update. All the ones I need are created on Awake/Start whatever. I’m just adding/removing them to/from a list on Update if a condition is met so that I can put certain functionality on them.

Depends on the size of the list, but almost certainly not. Don’t worry about it being a performance issue until it is a performance issue, at which point you can refactor.

1 Like

what if it is like 7 at largest?

I don’t think you’d notice if it was 700, maybe even 7000, to be honest. Don’t sweat the small stuff.

My only suggestion would be, depending on what you’re using this for, is to try to update your data during the meaningful “events” rather than polling for changes every frame whenever possible. Rather than looping through every object and removing it if it’s health is below 0, instead remove it when they actually take the damage that killed them.

1 Like

Thank you!