- Home /
Bad answer given causing question to be ignored
Best way to add specific child objects to a list.
In my game, I have spaceships. These spaceships have one to many child objects, some of which contain weaponsets. I would like to know the best way to have only the objects that have weaponset scripts attached, added to a list automatically (ie during game play if weapon is added/removed ect)
Ideally in c# please and thankyou.
Answer by rutter · Sep 05, 2014 at 04:06 AM
This question is pretty vague. The best data structure to use depends on the data itself: what does it mean, how will it be used? The simplest thing you could do is list all components of type X using GetComponentsInChildren, then iterate over the list according to whatever criteria you have in mind.
Maybe you want a List, for flexibility.
Maybe you want a HashSet, to make sure entries are only present once.
Maybe you want a Dictionary, for rapid lookups.
Maybe you want a jagged or multi-dimensional array, to represent some columnar or grid setup.
There are many data structures you could use. A question as open-ended as this is really more suited to the forums.
Failing that, you can always check the Unity wiki: Which Kind Of Array Or Collection Should I Use?
Yes I want a List as specified in the title. Is iterating through collected children the only way this can be done?