Unity - Scripting API: GameObject.FindGameObjectsWithTag
Success!
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
Submission failed
For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Declaration
public static GameObject[] FindGameObjectsWithTag(string tag);
Parameters
| Parameter | Description |
|---|---|
| tag | The name of the tag to search for GameObjects by. |
Description
Retrieves an array of all active GameObjects tagged with the specified tag. Returns an empty array if no GameObjects have the tag.
Tags must be declared in the tag manager before using them. A UnityException will be thrown if the tag does not exist or if an empty string or null is supplied as the tag parameter.
// Instantiates respawnPrefab at the location // of all GameObjects tagged "Respawn".using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { public GameObject respawnPrefab; public GameObject[] respawns; void Start() { if (respawns == null) respawns = GameObject.FindGameObjectsWithTag("Respawn");
foreach (GameObject respawn in respawns) { Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation); } } }
Another example:
Another example, testing for empty array: