static function FindGameObjectsWithTag (tag : string) : GameObject[]
Description
Returns a list of active GameObjects tagged tag. Returns null if no GameObject was found.
Tags must be declared in the tag manager before using them.
var respawnPrefab : GameObject;
var respawns =
GameObject.FindGameObjectsWithTag (
"Respawn");
for (
var respawn
in respawns)
Instantiate (respawnPrefab, respawn.transform.position, respawn.transform.rotation);
using UnityEngine;
using System.Collections;
public class example :
MonoBehaviour {
public GameObject respawnPrefab;
public GameObject[] respawns =
GameObject.FindGameObjectsWithTag(
"Respawn");
void Awake() {
foreach (GameObject respawn
in respawns) {
Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation);
}
}
}
import UnityEngine
import System.Collections
class example(
MonoBehaviour):
public respawnPrefab as GameObject
public respawns as (GameObject) =
GameObject.FindGameObjectsWithTag('Respawn')
def
Awake():
for respawn as GameObject
in respawns:
Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation)
Another Example
print(FindClosestEnemy().name);
function FindClosestEnemy () : GameObject {
var gos : GameObject[];
gos =
GameObject.FindGameObjectsWithTag(
"Enemy");
var closest : GameObject;
var distance =
Mathf.Infinity;
var position = transform.position;
for (
var go : GameObject
in gos) {
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}
using UnityEngine;
using System.Collections;
public class example :
MonoBehaviour {
GameObject FindClosestEnemy() {
GameObject[] gos;
gos =
GameObject.FindGameObjectsWithTag(
"Enemy");
GameObject closest;
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;
}
void Awake() {
print(FindClosestEnemy().name);
}
}
import UnityEngine
import System.Collections
class example(
MonoBehaviour):
def FindClosestEnemy() as GameObject:
gos as (GameObject)
gos =
GameObject.FindGameObjectsWithTag('Enemy')
closest as GameObject
distance as single =
Mathf.Infinity position as
Vector3 = transform.position
for go as GameObject
in gos:
diff as
Vector3 = (go.transform.position - position)
curDistance as single = diff.sqrMagnitude
if curDistance < distance:
closest = go
distance = curDistance
return closest
def
Awake():
print(FindClosestEnemy().name)