Hello… I have that dilemma about using list or arrays… Here s the context:
I have those game objects randomly spawning at fixed location. I have that spawnSpots array populated with instances of a SpawnSpot prefab that are placed at different points of the map.
Now, i want to prevent spawning on an already occupied SpawnSpot.
My basic idea is to have SpawnSpot removing itself from the spawnSpots array when it is occupied (and re adding itself when it becomes available again). But apparently arrays contains a fixed number of elements, so it may not be easy to remove and add element to it and i should change my spawnSpot array for a list, which is supposed to be more flexible. But, also apparently, arrays are more efficient in terms of performences… So here is my question:
- Should i go for a list instead of the array (according that there will never be more than, say, 16 SpawnSpots, maximum)? or stick to the array?
Alternatively, would it be possible to filter objects in the array, say by tag? (so i wouldnt have to bother about removing/adding the available objects from the array, but just tag them as “good for selection”)?
And finally, please dont write me any code… Just point me to the right direction
first of all, performance is not in the least a reason for choosing one over the other in your case. it’s not a tight loop or anything. In your case it’s just a matter of convenience.
you could have an array of bools in parallel you toggle. you could have a list, actually two to not forget choices… puck your favorite.
Object pooling -
See the code on this page, download his sample project.
He uses a List, the key to performance is actually pre-insantiating all the objects, and not creating them and destroying them in the game. He does it in code, but you could just use the Inspector to create all the spawn points if they’re in fixed positions.
you can index the objects in the array and pop and push like a stack (as in computing science definition of stack) from my memory, so may be no need to tag the objects.
I think the idea is you have 16 spawn spots inactive at start of game, then activate them one by one, deactivate when you need, then if you need more than 16 at some point, you increase size of list.
Yes you can always filter by tag, if (x.tag == “spaceship”) or whatever, but then you have to remember to always have the tag there, plus safety line of code in case tag missing to stop it crashing, so if you can do what you need with just the array or list index that’d be even better.
I’d google “Unity array list use cases” to see any pointers, but the idea is to use profiler when game gets to certain size (near the end stage of development I think is the idea!) so you actually see which parts slow you down, and focus on them instead of wasting time splitting hairs over a size 16 array or list.
also Unity docs has a tutorial on object pooling but I couldn’t see where the code is on the page, so I chose this one as all the code is neatly displayed on page. I think it’s quicker with article than video in the long run to really digest the meaning of it all.