FindGameObjectsWithTag
Retrieves an array of all active GameObjects tagged with the specified tag. Returns an empty array if no GameObjects have the tag.
Read time 3 minutesLast updated 10 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
FindGameObjectsWithTag(string, List<GameObject>)
Retrieves an array of all active GameObjects tagged with the specified tag. Returns an empty array if no GameObjects have the tag.
public static void FindGameObjectsWithTag(string tag, List<GameObject> results)
Parameters
Remarks
Tags must be declared in the tag manager before using them. A will be thrown if the tag does not exist or if an empty string or is supplied as the parameter.
UnityExceptionnulltag// 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:
// Find the name of the closest enemyusing UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour{ public GameObject FindClosestEnemy() { GameObject[] gos; gos = GameObject.FindGameObjectsWithTag("Enemy"); GameObject closest = null; float distance = Mathf.Infinity; Vector3 position = transform.position; foreach (GameObject go in gos) { Vector3 diff = go.transform.position - position; float curDistance = diff.sqrMagnitude; if (curDistance < distance) { closest = go; distance = curDistance; } } return closest; }}
Another example, testing for empty array:
using UnityEngine;// Search for GameObjects with a tag that is not usedpublic class Example : MonoBehaviour{ void Start() { GameObject[] gameObjects; gameObjects = GameObject.FindGameObjectsWithTag("Enemy"); if (gameObjects.Length == 0) { Debug.Log("No GameObjects are tagged with 'Enemy'"); } }}
FindGameObjectsWithTag(string)
Retrieves an array of all active GameObjects tagged with the specified tag. Returns an empty array if no GameObjects have the tag.
public static GameObject[] FindGameObjectsWithTag(string tag)
Parameters
The name of the tag to search for by.
GameObjectsReturns
Type | Description |
|---|---|
| GameObject[] |
Remarks
Tags must be declared in the tag manager before using them. A will be thrown if the tag does not exist or if an empty string or is supplied as the parameter.
UnityExceptionnulltag// 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:
// Find the name of the closest enemyusing UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour{ public GameObject FindClosestEnemy() { GameObject[] gos; gos = GameObject.FindGameObjectsWithTag("Enemy"); GameObject closest = null; float distance = Mathf.Infinity; Vector3 position = transform.position; foreach (GameObject go in gos) { Vector3 diff = go.transform.position - position; float curDistance = diff.sqrMagnitude; if (curDistance < distance) { closest = go; distance = curDistance; } } return closest; }}
Another example, testing for empty array:
using UnityEngine;// Search for GameObjects with a tag that is not usedpublic class Example : MonoBehaviour{ void Start() { GameObject[] gameObjects; gameObjects = GameObject.FindGameObjectsWithTag("Enemy"); if (gameObjects.Length == 0) { Debug.Log("No GameObjects are tagged with 'Enemy'"); } }}