Legacy Documentation: Version 2018.1 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

GameObject.FindGameObjectsWithTag

Suggest a change

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.

Close

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.

Close

Cancel

Switch to Manual
public static method FindGameObjectsWithTag(tag: string): GameObject[];
public static GameObject[] FindGameObjectsWithTag(string tag);

Parameters

tagThe name of the tag to search GameObjects for.

Description

Returns a list of active GameObjects tagged tag. Returns empty array if no GameObject was found.

Tags must be declared in the tag manager before using them. A UnityException will be thrown if the tag does not exist or an empty string or null is passed as the tag.

#pragma strict
// Instantiates respawnPrefab at the location
// of all game objects tagged "Respawn".
public var respawnPrefab: GameObject;
public var respawns: GameObject[];
function Start() {
	if (respawns == null)
		respawns = GameObject.FindGameObjectsWithTag("Respawn");
	for (var respawn: GameObject in respawns) {
		Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation);
	}
}
// Instantiates respawnPrefab at the location
// of all game objects 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:

#pragma strict
// Find the name of the closest enemy
public function FindClosestEnemy() {
	var gos: GameObject[];
	gos = GameObject.FindGameObjectsWithTag("Enemy");
	var closest: GameObject = null;
	var distance: float = Mathf.Infinity;
	var position: Vector3 = transform.position;
	for (var go: GameObject in gos) {
		var diff: Vector3 = go.transform.position - position;
		var curDistance: float = diff.sqrMagnitude;
		if (curDistance < distance) {
			closest = go;
			distance = curDistance;
		}
	}
	return closest;
}
// Find the name of the closest enemy

using 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:

no example available in JavaScript
using UnityEngine;

// Search for game objects with a tag that is not used

public class Example : MonoBehaviour { void Start() { GameObject[] gameObjects; gameObjects = GameObject.FindGameObjectsWithTag("Enemy");

if (gameObjects.Length == 0) { Debug.Log("No game objects are tagged with 'Enemy'"); } } }

Did you find this page useful? Please give it a rating: