I was wondering if it is possible to Find a model or prefab within my project.
I have tried to find the prefab using Find() and FindWithTag() but for some reason both of these functions return null. I have given the prefab a unique name and also a unique tag.
Is there something I’m missing? I was hoping that I did not have to make a public variable in my script and drag the prefab onto this variable since I would like to get this prefab dynamically.
Along the same lines as prefabs, is it possible to find a model at runtime so that I can dynamically create my GameObjects when required.
No, you cannot get prefabs with find or by searching for tags. Those functions only work for objects that are in the scene and prefabs are not.
You’ve got two options:
Use a public variable. If you need to dynamically access prefabs from various scripts/instances, you can create a singleton class which you can use to access the prefab references.
You can use the Resources class to dynamically load assets from the “Resources” folder in your assets. This however defeats Unity’s ability to track usage.
However, I have found a way of doing this, but am unsure of the functions limitations. The function is called “FindObjectsOfTypeIncludingAssets”, this function is not described in the documentation. The function is exposed by the UnityEngine.Object class and has public access rights. Does this function work in all builds of Unity or does the function only work in the Editor?