Unity find object by name

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.

Submission 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.

Description

Finds a GameObject by name and returns it.

This function only returns active GameObjects. If no GameObject with name can be found, null is returned. If name contains a ‘/’ character, it traverses the hierarchy like a path name.

For performance reasons, it is recommended to not use this function every frame. Instead, cache the result in a member variable at startup. or use GameObject.FindWithTag.

Note: If you wish to find a child GameObject, it is often easier to use Transform.Find.

Note: If the game is running with multiple scenes then Find will search in all of them.

GameObject.Find is useful for automatically connecting references to other objects at load time; for example, inside MonoBehaviour.Awake or MonoBehaviour.Start.

For performance reasons, it is recommended to not use this function every frame.

A common pattern is to assign a GameObject to a variable inside MonoBehaviour.Start, and use the variable in MonoBehaviour.Update.

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

Thanks for rating this page!

Is something described here not working as you expect it to? It might be a Known Issue. Please check with the Issue Tracker at

Thanks for letting us know! This page has been marked for review based on your feedback.

Читайте также:  Define int long long

If you have time, you can provide more information to help us fix the problem faster.

You’ve told us this page needs code samples. If you’d like to help us further, you could provide a code sample, or tell us about what kind of code sample you’d like to see:

You’ve told us there are code samples on this page which don’t work. If you know how to fix it, or have something better we could use instead, please let us know:

You’ve told us there is information missing from this page. Please tell us more about what’s missing:

You’ve told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:

You’ve told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:

You’ve told us there is a spelling or grammar error on this page. Please tell us what’s wrong:

You’ve told us this page has a problem. Please tell us more about what’s wrong:

Thanks for helping to make the Unity documentation better!

Is something described here not working as you expect it to? It might be a Known Issue. Please check with the Issue Tracker at issuetracker.unity3d.com.

Copyright © 2020 Unity Technologies. Publication: 2019.2-003K. Built: 2020-01-14.

First, I need to deactivate a game object and then after 10 seconds, activate it, so I thought coroutines are suitable:

Obviously, I can no longer use GameObject.Find so I use transform.Find to find the inactive game object, but of course .SetActive now does not work, since obj is not actually a game object.

Читайте также:  Brother utilities control center

How do I cast the found transform, so that it can be set active again?

I tried obj.gameObject.SetActive(true) but it must be wrong, because the object would not come back to life.

3 Answers 3

The problem is that Unity cannot find inactive GameObjects. GameObject.Find will only find active GameObject. You should either find and store the GameObject in a global variable or make the variable public then assign it from the Editor.

My solution uses a global variable then stores the GameObject in the beginner so that you don’t have to look for it again.

Below is a wrapper I made that finds GameObjects by name, tag or layer even if they are inactive.

You shouldn’t be using these every frame because they are slow. They are good if used in the Start or Awake function.

Find one GameObject:

USAGE:

Find in-active GameObject by Name:

Find in-active GameObject by Tag:

Find in-active GameObject by Layer:

Find all GameObjects (Notice the "s" in the Object from all the function names below):

I know that in Unity you can find an object of a certain type, using myObject = Object.FindObjectsOfType () , but that only returns the first object in the scene it finds of that type.

How do I make it find the object of a certain type and with a certain name?

2 Answers 2

There are different ways you would go about this depending on your particular circumstances.

Ultimately, you can not search for all active game objects based off both type and name, at the same time. You can easily search for all active game objects based off type, and search the resulting list for all game objects of type that use name. You also want to consider whether searching for game objects by tag would be more appropriate for your requirements.

Читайте также:  In its own right перевод

Below, I have provided two methods for searching for game objects by type and name. One can be easily used on the fly, the other is provided as a standalone method, and can be used more dynamically.

Find all GameObject(s) by type and name

To complete the search, simply search for objects based off type, and search through the resulting list for objects that match the name.

Note that you ask to "find objects", and I interpret that to literally mean you wish to have a resulting Object array. Any type you can look for, in this way, will be compatible with Object .

You could also retain the type you originally search for, in this way. In the above example, we could change List finalList to List with no problems. We could also use List finalList , and find the actual gameObject reference with finalList.Add(basicAIList[i].gameObject , if we wanted a list of game objects.

Consider that you may want to search like this multiple times. It might be more useful to have a standalone method that can perform the required search, and take in custom types and names.

We can then call this method as FindObjectsOfTypeAndName ("name to look for") .

I assume that we will always be looking for types that inherit from MonoBehaviour , but this will usually be the case, with custom types. If you need to change this, ensure that you also change the type of MonoBehaviour[] firstList .

Rate this post

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

Ваш адрес email не будет опубликован. Обязательные поля помечены *