- Home /
How to find layer instead of tags
OK, I want to do the same as the code is down but finding the layer This is what I want to do but with layer:
var walls = GameObject.FindGameObjectsWithTag("Walls");
//Important this part "s" because I am searching more than 1 object
So I am searching to do exactly the same(I mean also searching for more objects) but using layer so the question is, how can I do this?
Answer by Bunny83 · Apr 18, 2011 at 12:07 AM
You could use Physics.OverlapSphere with a real great radius to cover the whole scene. It will return only those GameObjects that have a Collider attached but at least you can specify a layermask. OverlapSphere does just a check against the objects AABB (axis aligned bounding box) so i guess it's quite fast.
thanks I will try to see if it work, but I think its going to work.
Answer by Eric5h5 · Apr 17, 2011 at 11:48 PM
There's no function for that, so you'd have to write your own function to get all game objects and loop through them to see if they're the desired layer, and make an array from the correct ones.
Oh :( that's to bad, but thanks any way to let me know, I think I would need to use tags after all
Answer by Martian-Games · Oct 10, 2012 at 09:47 PM
function FindGameObjectsWithLayer (layer : int) : GameObject[] {
var goArray = FindObjectsOfType(GameObject);
var goList = new System.Collections.Generic.List.<GameObject>();
for (var i = 0; i < goArray.Length; i++) {
if (goArray[i].layer == layer) {
goList.Add(goArray[i]);
}
}
if (goList.Count == 0) {
return null;
}
return goList.ToArray();
}