- Home /
How to find one object in specific layer?
For my game work, I need to separate the objects with the same tag and check of both are the same. How to find one object in specific layer?
Answer by giulio-pierucci · Feb 11, 2015 at 11:19 AM
Post a shot with scene Hierarchy please.
You shouldn't use FIND function. One valid procedure is:
On Start() of these objects, they call a method REGISTER(object) on another class (controller). objects are added to a list or dictionary. On Destroy you can DEREGISTER
When you need to check object proprieties, use the list. If you need to filter by layer, you may use a dictionary
By the way, using unity tools, you can use GameObject.FindGameObjectsWithTag
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public GameObject respawnPrefab;
GameObject[] respawns;
void Start() {
respawns = GameObject.FindGameObjectsWithTag("Respawn");
foreach (GameObject respawn in respawns) {
if(respawn.layer == 0)
Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation) as GameObject;
}
}
}
Your answer
Follow this Question
Related Questions
Is it possible to have multiple tags (or the like) on one object? 10 Answers
Accessing all GameObjects with a certain tag 1 Answer
how do i create a game object array with gameobjects of multiple tags 1 Answer
Find child of a Game Object using tag 4 Answers
Often when loading level the Layers i have on gameobjects reset to default. Please help? 1 Answer