Dynamic creation of prefabs at runtime


I want to know if it is possible to create temporary prefabs at runtime from the code.

I'm in a situation where I want artists to have access to a cut down prefab that contains a mesh, a renderer with assigned materials and and the animation script.

I can say Resource.Load to get access to that prefab.

At some point I want to call network instantiate on the prefab, however before I do I want to add NetworkViews and various other controller scripts from the code, this way the artists will only see the bare minimum.

I can't just say .AddComponent<>() to directly add things to the prefab I have just loaded, because that is the prefab that exists in the project, I think doing that would permanently add those components to the prefab. I think that was I need to achieve this would be to somehow create a new temporary prefab in memory.

Can this be done?


If it were me, I would Instantiate(prefab), add components, then SetActiveRecursively(false) the object so that it doesn't exist in the scene.

Assuming you have a reference to it, you can then Instantiate the modified prefab at your leisure