- Home /
getting multiple items by name/property
Setup: I have a game where I have set up a series of prefabs, and these prefabs all have nodes (empty GameObjects for now), and some of them have SpawnPoints (workaround for nested prefabs that simply instantiate a given GameObject). all of the nodes are currently named Node#, and I can put them on their own layer, or give them a certain Tag if that will help.
desired outcome: I want to search through a GameObject, and find all items of a given name
, or have a given Tag
so that I can register them for access by other things.
Information: I know that GameObject.Find(string name) is slow, and only returns the first thing it finds, and GameObject.FindWithTag(string Tag) aslo only returns the first thing it finds, but is faster.
I don't want to have to give every node
, or SpawnPoint
a specific layer mainly because of a cheviot of the level will by dynamically/randomly generated, so I will not know how many of these things there are. there will be constraints like knowing the dimensions of a given subspace of the level (room, and I am more interested in what is in each room then in the overall level)
Question: how do I effectively find all objects of a given name, or property within a given space that are all defined (exist as members of the prefab object)
Edit: these rooms will be chosen, and placed pseudo-randomly with a unknown amount, configuration, and type.
(following @## refers to a given room prefab/rule) one configuration could have: 5-R02, 2-R06, 7-C01, and 4-C02. this could be a small configuration, but can be seen as a trivial case. while another configuration could have: 1-R01, 1-R02, 3-R04, 8-R06, 18-C01, and 2-C04. which might be a larger, and more complex case, but still feasible with the system.
Answer by Wisearn · Jun 10, 2012 at 07:22 AM
http://unity3d.com/support/documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html
You should be able to get an array from this.
alright that answers half of the question, but is there a specific way to limit it to search inside a specific GameObject, or region?
I kinda don't get what you mean by "items" "in" a gameobject, you mean the components of a gameobject? or the children of a parent gameobject? or objects within a specific space/area of the scene?
If it's like space/area, maybe have a trigger object that envelops the area that manipulates tags on enter/stay/exit.
these will be children. for example: I have a room prefabe in the shape of an L that has nodes (Empty GameObjects), and spawnPoints (Empty GameObjects with a spawning script). I want to only get the nodes, spawnPoints, and/or entities (will not be children, but instantiated by the script on the spawnPoints) of that specific room so that I can log them, and track them.
I don't think the "having a trigger/volume to change tags" will work as these rooms will be chosen, and placed pseudo-randomly with a unknown amount, configuration, and type.
If you only have to do this once, you can perform a deep search using
GetComponentsInChildren<Transform>();
Then, you can check each one individually. This is not ideal, speed-wise, but if you can cache the results of it you only need do it once.
Well you could include the trigger area in the rooms that will be chosen, just like any other part of the room though? in the prefab
GetComponentsInChildren would only work for children, and not your entities that won't be.
Your answer
