# Are prefabs the only way to dynamically build a world in Unity?

I'm trying to understand how to setup my Unity prefabs and scripts to dynamically spawn a series of "encounters", with each encounter containing several GameObjects.

For example, imagine spawning the next several portions of an infinite-runner level. One encounter is a ramp, the other encounter is a pit with some coins.

This is what I am currently doing:

1. A global SceneManager script has a handle to an EncounterFactory prefab. When needed, the SceneManager will begin constructing the game world.
2. The EncounterFactory prefab contains a script that has a method SpawnRandomEncounter. The EncounterFactory also contains handles to several other prefabs for the objects that can be found in an encounter.
3. The SpawnRandomEncounter method uses the prefabs linked to EncounterFactory and builds the expected GameObjects.

So when building out a new "encounter", the SceneManager clones the EncounterFactory (which serves as the parent object for the full encounter), and then calls SpawnRandomEncounter to build out the child objects.

This seems suboptimal. First, I need to have myriad prefabs for each thing that could be spawned. It also requires a meta-prefab to EncounterFactory. That is, a prefab who just allows me to spawn other prefabs.

Is there a better way to dynamically build out a game world or level in Unity? Or is it prefabs upon prefabs upon prefabs?

For example, I'm aware that I can find an existing object by name. (e.g. find EncounterTemplate5 and clone that.) However, that requires that EncounterTemplate5 exist in the game world. (So it can be cloned.)

First off, do you know about having a Resources folder? Assets put in there can be loaded in code by name, instead of needing to reference them within the scene. Putting dynamically loaded assets in a folder called "Resources" makes dynamically loading assets a lot more flexible.

The assets that you load from Resources don't have to be prefabs, although you usually want them to be in order to already have their various components and whatnot already setup. However if making everything a prefab is onerous for some reason then you could load the obj mesh (or whatever) and then AddComponent

• I am aware of the Resources folder, but all the documentation says that it will kill performance. Is it used in practice? Jan 20 '14 at 23:10
• @Chris You have to understand that a lot of Unity objects are what we call, "Lazy Loaded", prefabs included. Prefabs are one of the building blocks of Unity Game Development, so yes - they are used in practice. If you don't want to use prefabs for some reason consider using an Asset Bundle.
– Grey
Jan 20 '14 at 23:20
• It's also worth mentioning that GameObject.Find is as expensive as Resources.Load - all relative to their search area's size. So you're best bet is to use an Asset Bundle, imo. You can provide the exact area to search for the asset and simply stream it into your game ;-)
– Grey
Jan 20 '14 at 23:26
• Realize that terms like "kill performance" (and related terms like "slow") are all relative. Often things which are too slow to do hundreds of times per frame (eg. while spawning particles) are just fine to do once during the pause for loading a new level. Jan 20 '14 at 23:59
• Another note is that if you are spawning multiples of an object (say ten crates, or enemies, or whatever) you'll only want to load the resource once. Cache that and instantiate from it multiple times rather than reloading (truthfully I would actually be fairly surprised if it doesn't cache internally, anyway). You might also consider, for commonly used objects, loading them up at game start and caching the reference in your scene manager. Mar 16 '15 at 21:07