I know that in Unity you can find an object of a certain type, using myObject = Object.FindObjectsOfType<MyType>(), 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?

share|improve this question
    
No, the method you've shown, FindObjectsOfType (with Objects pluralised) returns an array of all such objects found. You can then iterate over the array to find one with a matching name. But please don't. ;) – DMGregory Nov 3 '16 at 19:22
    
I didn't really get why not (and by the way it's not a GameObject that i'm looking for). Is there any other way than iterating through the array or that's it? – Gambit2007 Nov 3 '16 at 19:23
    
It's a working solution - I'm being a bit hyperbolic. But string-based lookups are generally more error-prone than other strategies, and can lead to unintuitive errors & exotic workarounds like the one in that tweet (this code depended on another script that looked up objects by name, which would select the old destroyed copy before it was deleted instead of the newly-spawned copy, leading to missing reference errors on the next frame). In my experience in Unity there's always more reliable & robust methods available. If you describe your use case in more detail we may be able to suggest some. – DMGregory Nov 3 '16 at 19:35
    
I'll try the array iteration first since it's a very small scene with only 2 objects of the same type that i have to tell the difference between. Thanks! – Gambit2007 Nov 3 '16 at 19:38
up vote 0 down vote accepted

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.

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.

using UnityEngine;
using System.Collections.Generic;

...

    TypeToLookFor[] firstList = GameObject.FindObjectsOfType<TypeToLookFor>();
    List<Object> finalList = new List<Object>();
    string nameToLookFor = "name of game object";

    for(var i = 0; i < firstList.Length; i++)
    {
        if(firstList[i].gameObject.name == nameToLookFor)
        {
            finalList.Add(firstList[i]);
        }
    }

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<Object> finalList to List<TypeToLookFor> with no problems. We could also use List<GameObject> 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.

...

public List<Object> FindObjectsOfTypeAndName<T>(string name) where T : MonoBehaviour
{
    MonoBehaviour[] firstList = GameObject.FindObjectsOfType<T>();
    List<Object> finalList = new List<Object>();

    for(var i = 0; i < firstList.Length; i++)
    {
        if(firstList[i].name == name)
        {
            finalList.Add(firstList[i]);
        }
    }

    return finalList;
}

We can then call this method as FindObjectsOfTypeAndName<TypeToLookFor>("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.

share|improve this answer

You can use GameObject.FindObjectsWithTag("tag") function instead

share|improve this answer
    
This solution finds objects of tag, but ignores both type and name. – Gnemlock Nov 3 '16 at 22:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.