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;
}
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.
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