言語: 日本語
  • C#
  • JS
  • Boo

スクリプト言語

お好みのスクリプト言語を選択すると、サンプルコードがその言語で表示されます。

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

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static function FindGameObjectsWithTag(tag: string): GameObject[];
public static GameObject[] FindGameObjectsWithTag(string tag);
public static def FindGameObjectsWithTag(tag as string) as GameObject[]

Parameters

tag GameObjects を検索するためのタグ名

Description

/tag/でタグ付けされたアクティブなゲームオブジェクトのリストを返します。もし見つからない場合は空配列を返します

この関数を使用する前にタグマネージャーでタグの設定を行う必要があります。タグが存在しない、または空文字や null を渡した場合は UnityException の例外が発生します。

	// Instantiates respawnPrefab at the location 
	// of all game objects tagged "Respawn".

	var respawnPrefab : GameObject;
	var respawns;
	function Start()
	{
		if (respawns == null)
			respawns = GameObject.FindGameObjectsWithTag ("Respawn");
		for (var respawn in respawns)
			Instantiate (respawnPrefab, respawn.transform.position, respawn.transform.rotation);
	}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public GameObject respawnPrefab;
    public Object respawns;
    void Start() {
        if (respawns == null)
            respawns = GameObject.FindGameObjectsWithTag("Respawn");
        
        foreach (Object respawn in respawns) {
            Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation) as GameObject;
        }
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	public respawnPrefab as GameObject

	public respawns as Object

	def Start() as void:
		if respawns == null:
			respawns = GameObject.FindGameObjectsWithTag('Respawn')
		for respawn as Object in respawns:
			(Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation) as GameObject)

他の例:

	// Print the name of the closest enemy
	print(FindClosestEnemy().name); 
	
	// 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;	
	}
using UnityEngine;
using System.Collections;

public class ExampleClass : 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 Example() {
        print(FindClosestEnemy().name);
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def FindClosestEnemy() as GameObject:
		gos as (GameObject)
		gos = GameObject.FindGameObjectsWithTag('Enemy')
		closest as GameObject
		distance as float = Mathf.Infinity
		position as Vector3 = transform.position
		for go as GameObject in gos:
			diff as Vector3 = (go.transform.position - position)
			curDistance as float = diff.sqrMagnitude
			if curDistance < distance:
				closest = go
				distance = curDistance
		return closest

	def Example() as void:
		print(FindClosestEnemy().name)

他の例: 空配列のテスト

    // 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");
        }
}