- Home /
How to convert gameobject into prefab at runtime?
I am trying to convert a gameobject into a prefab to a specific folder using code. Basically, when the user hits the done button, a gameobject should be converted into a prefab to a specific folder from where those prefabs will be sent to firebase storage. I didn't find any way to do it. Is it even possible?
Answer by N-8-D-e-v · Sep 16, 2020 at 11:37 AM
It is indeed, impossible, BUT there is a pretty simple workaround. Say you want to create a prefab at runtime of skyscraper B. Save a prefab of Skyscraper A, then put one in your scene, and set it to inactive. Then, edit that object at runtime, and just instantiate whenever, so it acts like a prefab.
Thanks for your reply N8. The solution you have given can't be applied in my game. But I have found a way to do it with the help of editor script. PrefabUtility.SaveAsPrefabAsset();
But you said you want to do this at "runtime" and that "users" should be able to do this. Editor scripts only work inside the Unity editor. So your usecase is quite confusing. Prefabs are editor only features. They do not exist at runtime. Prefabs are just predefined objects that are not part of the scene and can be used as cloning source.
You are right. Why I didn't thought about that. I am new to editor script. I think I need to find some other way to solve my problem.
Answer by Liamono · Sep 21, 2020 at 05:21 PM
The best way to do this is to set up a custom save system that uses .bin, .json, or .txt files. You'll need to have a script that can create a .bin, .json, or .txt file on disk, and then have the GameObjects re-construct themselves upon instantiation (via Start(), Awake(), etc) using the custom file. There's no way to save a prefab directly in a built game, but using .bin/.json/.txt allows you to achieve the same effect for almost any aspect of any game.
Your answer

Follow this Question
Related Questions
Multiple Prefab in Object Pool or One Prefab With Add Component 0 Answers
Reinitialize prefab 0 Answers
Dragging to Add GameObjects 0 Answers