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.
Close Submission failedFor 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.
Close Your name Your email Suggestion* Switch to ManualDeclarationpublic 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:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4