Find inactive objects with script

I’m trying to create a system that will find all the items in-game.

But some items are turned off when the game is loaded (pickuped items) SetAtive(false)

And now when I try to find those items I get null GameObject[] objects = Resources.FindObjectsOfTypeAll(typeof(Item)) as GameObject[];

NullReferenceException: Object reference not set to an instance of an object

Code:

    GameObject GetItemGameObject(Item item)
    {
        GameObject[] objects = Resources.FindObjectsOfTypeAll(typeof(Item)) as GameObject[];
      
        GameObject itemObj = new GameObject();

        foreach (var i in objects)// -NULL
        {
            if(i.GetComponent<Item>().universalID == item.universalID)
            {
                itemObj = i.gameObject;
            }
        }
        return itemObj;
    }
1 Like

I’d recommend just adding all these items to a list when they are created. Either when they are instantiated, or in a Start method attached to the item. They will remain in the list when they are inactive, so you’d just iterate over the list.

You’re casting the result to GameObject[ ] for a type of Item. Use Item[ ] instead. Also when you call new GameObject() you’ll instantiate a new GameObject in the scene. Lastly, once you’ve found the item you can break out of the loop.

GameObject GetItemGameObject(Item item)
{
    Item[] items = Resources.FindObjectsOfTypeAll(typeof(Item)) as Item[];
  
    GameObject itemObj = null;

    foreach (var i in items)
    {
        if (i.universalID == item.universalID)
        {
            itemObj = i.gameObject;
            break;
        }
    }

    return itemObj;
}
1 Like

Use this instead Unity - Scripting API: Object.FindObjectsOfType

Item[] items = FindObjectsOfType<Item>(true);
3 Likes

This work. Thanks !

FindObjectsOfType Returns a list of all active loaded objects of Type type.
so this is of no use to me…

Explain further why you think it’s of no use to you?

https://docs.unity3d.com/ScriptReference/Resources.FindObjectsOfTypeAll.html

They both return the same thing, except with FindObjectsOfType you get to pass in a parameter saying “give me inactive objects too”.

FindObjectsOfType cant find deactivated object in Hierarchy!

Yes, yes it can. I linked you to the docs page for it which tells you it can, AND gave you example code with how to do it.

5331801--537216--Annotation 2020-01-02 212716.png

I was curious about this, because FindObjectsOfType has not been able to return inactive gameobjects before. It seems the 2019.2 docs listed this param, but their code itself still doesn’t incorporate it. I’m using Unity 2019.3 and when I check possible options, the param version isn’t available. Even if I include the true bool (as a test), it throws an error. Even Unity’s doc doesn’t show they take the bool as a param. Maybe this is for a future update, but for now, it appears resources is the one that has to be used.

4 Likes

Like I said above. this function does not return disabled objects. It might say that in the documentation but it doesn’t work (Unity 2019.3.0f1).

1 Like

And I was verifying it since it is now listed in the docs as taking the bool param. It’s certainly a much requested feature.

2 Likes

i found a much eiser way for if you want your item to be picked up have it starting not bieng parented to the item manager the gameobject which is the parent to the items and when you pickup the item it makes it its parent using this:

if (activateTrigger)
{
item.SetActive(true);
item.transform.SetParent(parent);

}
parent is a public transform which you would specify which parent you want the item object to be a child to

Make object active in inspector, then write on Void Start() gameObject.setActive(false)
then make GameObject.Find() in Void Awake() This function is the same but called before start