Как найти объект по тегу юнити

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

Your name

Your email

Suggestion*

Cancel

Switch to Manual

Declaration

public static GameObject FindWithTag(string tag);

Parameters

tag The tag to search for.

Description

Returns one active GameObject tagged tag. Returns null if no GameObject was found.

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

Note: This method returns the first GameObject it finds with the specified tag. If a scene contains multiple active GameObjects with the specified tag, there is no guarantee this method will return a specific GameObject.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public GameObject respawnPrefab; public GameObject respawn; void Start() { if (respawn == null) respawn = GameObject.FindWithTag("Respawn");

Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation); } }

Приветствую Вас, дорогие разработчики. В данной статье мы научимся искать игровые объекту по его имени, и по тегу. Ведь при разработке игр нам довольно часто нужно находить игровые объекты, для дальнейшего выполнения каких-либо действий. Приступим сразу к делу.

Поиск объекта по имени

Для того, чтобы найти объект по его имени, необходимо воспользоваться методом GameObject.Find. Посмотрим реализацию данного метода на примере:

public GameObject obj;

void Awake()
{
    obj = GameObject.Find("NameGameObject");
}

Обратите внимание, что в первой строке мы создали переменную obj, чтобы в ней хранить ссылку на игровой объект. А в пятой строке, воспользовавшись методом GameObject.Find, получили сам объект по его имени.

Поиск объекта по тегу

Теперь давайте получим ссылку на наш объект по тегу. Для этого воспользуемся методом GameObject.FindGameObjectWithTag:

public GameObject obj;

void Awake()
{
    obj = GameObject.FindGameObjectWithTag("TagName");
}

Как видите, смысл такой же, как и в предыдущем примере. Всё очень просто. Только не забудьте указать тег для Вашего объекта, перед запуском данного скрипта. Иначе объект найден не будет.

Важно: Если в Вашей сцене находится несколько объектов с одинаковыми тегами, то метод GameObject.FindGameObjectWithTag найдёт только самый первый объект. Поэтому, существует ещё один способ, который позволит найти все объекты по указанному тегу.

Поиск массива объектов по тегу

Чтобы найти массив объектов по тегу, воспользуемся методом GameObject.FindGameObjectsWithTag. Вы наверное обратили внимание, что название данного меотда почти 1 в 1 схоже с предыдущим методом, за исключением добавленной быквы s в середине названия.

Рассмотрим пример использования метода GameObject.FindGameObjectsWithTag:

public GameObject[] obj;

void Awake()
{
    obj = GameObject.FindGameObjectsWithTag("TagName");
}

Обратите внимание, что при использовании метода GameObject.FindGameObjectsWithTag, в первой строке мы должны оказать, что нам нужен массив объектов, при помощи квадратных скобок [].

Example

Tags make it particularly easy to locate specific game objects. We can look for a single game object, or look for multiple.


Finding a Single GameObject

We can use the static function GameObject.FindGameObjectWithTag(string tag) to look for individual game objects. It is important to note that, in this way, game objects are not queried in any particular order. If you search for a tag that is used on multiple game objects in the scene, this function will not be able to guarantee which game object is returned. As such, it is more appropriate when we know that only one game object uses such tag, or when we are not worried about the exact instance of GameObject that is returned.

///<summary>We create a static string to allow us consistency.</summary>
string playerTag = "Player"

///<summary>We can now use the tag to reference our player GameObject.</summary>
GameObject player = GameObject.FindGameObjectWithTag(playerTag);

Finding an Array of GameObject instances

We can use the static function GameObject.FindGameObjectsWithTag(string tag) to look for all game objects that use a particular tag. This is useful when we want iterate through a group of particular game objects. This can also be useful if we want to find a single game object, but may have multiple game objects using the same tag. As we can not guarantee the exact instance returned by GameObject.FindGameObjectWithTag(string tag), we must instead retrieve an array of all potential GameObject instances with GameObject.FindGameObjectsWithTag(string tag), and further analyse the resulting array to find the instance we are looking for.

///<summary>We create a static string to allow us consistency.</summary>
string enemyTag = "Enemy";

///<summary>We can now use the tag to create an array of all enemy GameObjects.</summary>
GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag );

// We can now freely iterate through our array of enemies
foreach(GameObject enemy in enemies)
{
    // Do something to each enemy (link up a reference, check for damage, etc.)
}

Introduction

This arti­cle belongs to the series about find­ing GameOb­jects and Com­po­nents ref­er­ences from the scene in Uni­ty, in this series we will see dif­fer­ent tech­niques to find from a Script any object that is in the hier­ar­chy in a cer­tain scene in Uni­ty, this is some­thing very impor­tant to under­stand and know how to do it because if we have the ref­er­ence of an object or com­po­nent, we can access it and read any pub­lic data or exe­cute any pub­lic func­tion we need.

Here you have the playlist about find­ing the ref­er­ences in Uni­ty, in that list we will be adding videos with more and more spe­cif­ic tech­niques. The fol­low­ing video talks about the tech­nique shown in this article:

In the following video we see how to create a code to assign GameObjects and components in the inspector in UNITY.

Initial conditions

We start from a Script called “Find­Ref­er­ence­O­fAnOb­ject” in which we are going to find the ref­er­ence of a cer­tain GameOb­ject that is in the scene in Uni­ty, inside the Script we will use that ref­er­ence to print its name in con­sole with the instruc­tion of the line 13 of the fig­ure 1.

script to show ways to find gameobjects references in unity scene
Fig. 1: Script we are going to use to see the dif­fer­ent meth­ods to find ref­er­ences of objects from the scene in Unity.

The hier­ar­chy of the scene that we are going to use is com­posed by the GameOb­jects that are shown in fig­ure 2, the object “Script-GameOb­ject” is the one that has the Script in fig­ure 1 assigned and it is the one that will be in charge of find­ing the ref­er­ences, in fig­ure 3 you can see the inspec­tor of this GameOb­ject, where the Script is assigned.

The “GDT (Object to Find)” object seen in fig­ure 2 is the object we want to find from the script, so if we suc­ceed we should see the name of this object print­ed on the console.

One of the ele­ments that every GameOb­ject has in Uni­ty is a Tag, it is locat­ed in the inspec­tor head­er, as we can see in fig­ure 4. We can use this tag to find the ref­er­ence of the GameOb­ject of the scene in our Script.

By default the GameOb­jects will have assigned the “Untagged” Tag, but we can assign one of the pre­de­fined Tags or cre­ate new Tags, in fig­ures 4 and 5 I show how to cre­ate a new Tag in Unity.

Once we have cre­at­ed the Tag we have to assign it to the object, we select again the GameOb­ject in the inspec­tor, deploy the Tag drop-down menu and choose it, as shown in fig­ure 6.

assigning a tag to a gameobject in unity to find the reference of a gameobject in the scene from a script
Fig. 6: The cre­at­ed Tag is assigned to the object to be ref­er­enced in the Script.

Now we are able to find that GameOb­ject using the Tag, for this we exe­cute the “Find­GameOb­jectWith­Tag” method from the GameOb­ject class and we pass as para­me­ter the name of the Tag that we want to search. In fig­ure 7 we see this instruc­tion in line 16, notice that the name of the Tag has been defined in a String in line 11 and then in line 16 we pass as para­me­ter that variable.

Instruction to find the reference of a GameObject of the scene through its Tag in Unity
Fig. 7: Instruc­tion to find the ref­er­ence of a GameOb­ject in the scene through its Tag.

When this instruc­tion is exe­cut­ed, Uni­ty will check all the objects in the hier­ar­chy until it finds a GameOb­ject that has that tag assigned, at that point it returns the object and it is stored in the “object­ToFind” field. If there is no object with that Tag, the “object­ToFind” field will have a null value.

To keep in mind, if we have more than one GameOb­ject that has the same Tag assigned, Uni­ty will return us the first of them that it finds in its reg­istry, in this case some ambi­gu­i­ties could hap­pen, we could obtain the ref­er­ence of an object dif­fer­ent from the one we want.

How to find gameobjects in Unity3D

Finding and referencing a game object properly in Unity3D is one of the most asked questions for those who are new to Unity3D. In this article, I will write about referencing game objects, finding them according to their names, tags, and types. But let me give you the answer to the question at the title briefly.

In Unity3D, we can find game objects according to their names, tags, and types. For these purposes, we use the following methods respectively: GameObject.Find( ), GameObject.FindWithTag( ) and Object.FindObjectOfType(). These methods will return only one game object. Additionally, it is also possible to find all the game objects that have the same tag or are in the same type, using GameObject.FindGameObjectsWithTag( ) and Object.FindObjectsOfType( ). These methods will return arrays of game objects.

Contents

  • Referencing game objects in Unity3D
    • What is referencing?
    • How do you reference a game object in Unity3D?
  • How to find game objects by their names
  • Finding a game object that has a specific tag
  • Getting the array of all game objects that have the same tag
  • Finding a game object that matches a specific type
  • Getting the array of all game objects that matches a specific type
  • How to find a child game object
  • Further remarks

Referencing game objects in Unity3D

What is referencing?

Most of the time, we want to access other game objects and their components. Hence, we can gather information about them when it is required, send information to them, control their behaviors, or execute a method that is in a script that is attached to them. In these cases, we need an address(or let’s say a phone number) of the game object and thus, we can dial whenever we want. This is called referencing.

While developing games in Unity3D, we always reference other game objects or components. Therefore, this is a fundamental topic you should understand before you go further.

How to create references to game objects or components

First of all, we have to declare variables that will store the address of the game objects or components. And hence, whenever we would like to access the properties of these game objects or components, we use these variables.

GameObject myGameObject;
GameObject herGameObject;
GameObject hisGameObject;

In the code above, we declared two variables in type GameObject but we have not assigned any object them yet. You may consider this as if we are reserving empty rows in an address book that we will fill them out later.

To reference these variables, we have a couple of options. We can search the game object that we want to assign using built-in methods that are included in Unity. We will see this option in later sections of this article. Another option is to assign relevant game objects directly in the Unity editor. But to do this we have to declare the objects either public or private with [SerializeField] attribute.

GameObject myGameObject;
public GameObject herGameObject;
[SerializeField]private GameObject hisGameObject;

In the code above, the first variable is declared as private(you can put the private keyword in front of it as well if you want), the second variable is declared as public and the third variable is declared as private with [SerializeField] attribute. [SerializeField] attribute makes this variable visible in the editor but still inaccessible from other scripts.

Referencing in unity editor

Now, you can drag and drop the game objects, that you would like to assign, to the slots that are visible in the inspector.

If you would like to create references to the components that are associated with the game objects, you need to declare variables that are in the type of that component.

Rigidbody rigidbodyComponent;
BoxCollider boxColliderComponent;

How to find game objects by their names

As I mentioned above, we can create references to game objects and components by searching and finding them using built-in methods in Unity. This is useful especially when you want to stay the variable private or when you want to access an object that is created during runtime.

In order to search for a game object that has a specific name in the scene, we have to use the method GameObject.Find( ). It takes a string parameter. And this parameter is the name of the game object that we want to find.

myGameObject = GameObject.Find("Sphere");

In the code above, we created a reference for the game object that has the name “Sphere”.

If you would like to access a component that is attached to this game object, you should create a reference for that component. For instance, the following creates a reference for a rigidbody component that is attached to this game object, and hence you can access the properties of this component.

rigidbodyComponent = GameObject.Find("Sphere").GetComponent<Rigidbody>();

Finding a game object that has a specific tag

In addition to finding an object by its name, we can also find it by its tag which we are able to determine objects in the scene.

Finding a game object that has a specific tag

To find a game object that has a specific tag, we use the method GameObject.FindWithTag( ). This method takes a string parameter and searches for it (There is one more method that does the same job. You can also use GameObject.FindGameObjectWithTag( ) for the same purpose).

myGameObject = GameObject.FindWithTag("Player");

A tag can be used either for only one object or multiple objects. If there is more than one object that has the same tag, this method returns only one of them.

Getting the array of all game objects that have the same tag

If there are multiple objects that have the same tag and we want to get them all, we need to use the method GameObject.FindGameObjectsWithTag( ). Likewise, this method also takes a string parameter and returns an array of all game objects. Therefore, we have to declare an array that will store the game objects.

GameObject[] cubes;

The following returns and assigns all objects that have the tag “Cube”.

cubes = GameObject.FindGameObjectsWithTag("Cube");

Finding a game object that matches a specific type

We can also search and find a game object that matches a specific type. In other words, for instance, we can get a game object or component that has a specific component. To do this, we use the method Object.FindObjectOfType( ). This method also returns only one of the objects.

The following one finds a light component and creates a reference to it.

Light mainLight = (Light)FindObjectOfType(typeof(Light));

Getting the array of all game objects that match a specific type

If there are multiple game objects that match a specific type, we can find all of them and create references in an array. For this purpose, we use the method Object.FindObjectsOfType( ). This is an example of how we use it:

var sphereCollider = FindObjectsOfType(typeof(SphereCollider));

How to find a child game object

In order to find a child object and create a reference to that child object we can use the method Transform.Find( ). This method takes a string parameter and returns the child game object that the name matches this parameter.

Assume that there is a game object in the scene that has a tag “Player”. And also assume that this player object has a child game object which has the name “Gun”. If we want to create a reference for the “Gun” object, we may do this as the following:

GameObject gun=GameObject.FindWithTag("Player").transform.Find("Gun");

This is useful especially if there are multiple objects that has the same name under different objects.

Further remarks

In this article, we see different ways of how we reference game objects in Unity3D. Here, I need to warn you that searching and finding methods are extremely slow and you should avoid using them in Update, FixedUpdate, or LateUpdate methods, if possible. We generally use them in Start or Awake methods. You may also consider creating references in Singleton’s to improve performances. We have various tutorials about the Unity3D Engine that you can see in this link.

Добавить комментарий