I'm creating what is essentially my own prefab system. Game objects are defined by a human readable data file. I'd like to create an empty GameObject, load it with the components defined in the data file and have it ready and waiting for an Instantiate()
call. However, whenever I use GameObject go = new GameObject()
a new game object is added to the scene.
Using Unity built in prefabs, I can load a prefab as a GameObject, and it's not added to the scene. (See example below)
The rationale behind this is I'd like to have one list that contains GameObjects, some generated by loading Unity prefabs and others created by my custom prefab system. This needs to happen at run time, and can't involve the Unity Editor (since ideally end users would be able to define their own data files).
How can I create a new GameObject, without Unity automatically instantiating it for me?
For example, I can make a call like this:
Resources.LoadAll("PrefabsDirectory", typeof(GameObject))
And I'll get a list of GameObjects, none of which are added to the scene when the call is made. Essentially, I'm creating a LoadAll
method for my own prefabs, and likewise, I don't want to instantiate the GameObjects when the call is made.
Destroy
on the GameObject. Which removes it from the scene and releases it, making it unusable. \$\endgroup\$go.SetActive(false)
is the best way to create something like an object pool. It's not exactly what you want, but perhaps you could have a small group of inactive objects that are later cloned for your custom instantiation. \$\endgroup\$