- Home /
The question is answered, right answer was accepted
How to (in code) save a gameobject that already exists in my scene as a prefab? (c#)
Sorry if the answer to this is really obvious. I did some digging and while I found a lot of threads of people asking how to generate an object and save it as a prefab, or how to create a prefab, but not how to turn an object that already exists into a prefab. I'm basically asking how to move an object from my scene into a folder through code. Thanks!
there is a reason you might not find an answer to this. if there is an object that exists in your scene then simply just drag and drop the item from the hierarchy window down into your assets window and it becomes a prefab.
if you are generating obects through code then there should be no need for a prefab....just run the code again.
if you are adjusting a prefab during run time. you simply save the reference to the prefab and the adjustments.
if you can explain what you are trying to accomplish and what unity is not doing for you in your game i'm sure we can help.
Hey thanks for the response. yeah that is my problem. my case is hyper-specific to my project. I need to make an object I already generated at runtime into a prefab. No worries, though. I ended up modifying my code so that i create prefabs without necessitating that I create a gameobject first, so i guess my question is now moot.
Object prefab = EditorUtility.CreateEmptyPrefab ("name" + ".prefab");
This is the line of code that did the trick for me.
This is slightly off topic, but for anyone that stumbles on this in future, it's worth noting you can drag runtime generated GameObjects into the project explorer to create prefabs too.
I had procedurally generated the solar system, applying all names and textures etc... But didn't want that to run each time, so copied all the created GameObjects during runtime, stopped playback and then pasted into the scene.
Follow this Question
Related Questions
Link GUIText to a prefab? 2 Answers
How can I destroy a gameobject that is part of a prefab ? 0 Answers
Does my script have a memory leak ? 0 Answers