Legacy Documentation: Version 2017.1 (Go to current version)
言語English
  • C#
  • JS

Script language

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

GameObject.FindGameObjectsWithTag

フィードバック

ありがとうございます

この度はドキュメントの品質向上のためにご意見・ご要望をお寄せいただき、誠にありがとうございます。頂いた内容をドキュメントチームで確認し、必要に応じて修正を致します。

閉じる

送信に失敗しました

なんらかのエラーが発生したため送信が出来ませんでした。しばらく経ってから<a>もう一度送信</a>してください。ドキュメントの品質向上のために時間を割いて頂き誠にありがとうございます。

閉じる

キャンセル

マニュアルに切り替える
public static function FindGameObjectsWithTag(tag: string): GameObject[];
public static GameObject[] FindGameObjectsWithTag(string tag);

パラメーター

tag The name of the tag to search GameObjects for.

説明

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.

Another example:

Another example, testing for empty array:

no example available in JavaScript
// 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); } } }
    // Find the name of the closest enemy
    function FindClosestEnemy () : GameObject {
        // Find all game objects with tag Enemy
        var gos : GameObject[];
        gos = GameObject.FindGameObjectsWithTag("Enemy");
        var closest : GameObject;
        var distance = Mathf.Infinity;
        var position = transform.position;
        // Iterate through them and find the closest one
        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;
    }
// 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; } }
// Search for game objects with a tag that is not used

function Start () { var gos : GameObject[]; gos = GameObject.FindGameObjectsWithTag("fred");

if (gos.length == 0) { Debug.Log("No game objects are tagged with fred"); } }