- Home /
How to create/update Prefab in Runtime?
I am having a Gizmo app and I have a root GameObject with a prefab. I am able to duplicate the objects at runtime. I wanted to save user created objects into prefabs. How can I achieve this in Unity?
Answer by Bunny83 · Oct 31, 2017 at 02:17 AM
You can't. Assets are generally read only in build games. Furthermore prefabs are an editor feature. At runtime a prefab is just a serialized off-scene gameobject which you can clone into the scene. You can't create such off-scene objects at runtime. It also wouldn't make sense as those can't be stored anywhere as Unity doesn't have any built-in way to serialize gameobjects at runtime.
If you need something like prefabs at runtime you have to serialize them on your own into your own format. Unity's asset system / database is rather complex. If you want to mimic such an behaviour you should keep in mind what you need to serialize. The main problem are how to represent cross references between different serialized assets. A simple gameobject would consist of the actual gameobject / transform hierarchy tree but also any other components which might be attached to any of those objects. Furthermore those components can reference other assets like Materials, Meshes, Textures, ...
If you only want support built-in basic assets you may just want to provide them in arrays so you can simply store the index of the used Mesh / Material / Texture / .... However if you somehow want to support externally provided mesh data or texture data you have to store those as well where it gets really nasty...
Your answer
![](https://koobas.hobune.stream/wayback/20220612142225im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Creating a weapon database from prefabs 1 Answer
Changes done in prefab script get applied to all instances of that prefab. 0 Answers
Hiding gameobjects in hierarchy make it less expensive ? 0 Answers
Creating a scene: instantiating a prefab in a scene or use pre-instantiated prefabs? 1 Answer
Link GUIText to a prefab? 2 Answers