- Home /
How to save a prefab when a button is pressed
In my scene I have a small character customization mechanic where the player can change the head, torso and color scheme of the robot character. However I want the changes to save so it can be loaded in other scenes and when the app is closed it loads up the same instance how do i do this.
As you can see the droid from the current run-time is different from the droid prefab in the project. How do I overwrite the project droid with the current instance when the save button is pressed.
Answer by Cherno · Jun 01, 2015 at 06:03 PM
You can create prefabs at runtime with the PrefabUtility.CreatePrefab function. Take a look at the ReplacePrefab options available as a parameter.
Like this:
GameObject go;
string prefabPath = "Prefabs";
PrefabUtility.CreatePrefab("Assets/Resources/" + prefabPath + "/" + go.name + ".prefab", go, ReplacePrefabOptions.ReplaceNameBased);
The PrefabUtility class comes up as red in mono develop. Unity cant find it for some reason.
You need to put
#if UNITY_EDITOR
using UnityEditor;
#endif
at the start of the script.
However, I just realized that it's not possible to save prefabs in a Build, so you can disregard this answer.
You will have to save the relevant information regarding color, head etc. as variable values and use these to reconstruct the desired robot.
nvm i imported the unity editor which had the prefabutiltiy class in it
Your answer
Follow this Question
Related Questions
Terrain: Use as scenario (individual) and prefab (unchanged, reusable) 0 Answers
How do I find a Prefab after I Replaced said Prefab? 1 Answer
Visualization of saved games inside a Load Menu - Problem with instantiated prefabs 0 Answers
How to refference a Prefab in a saving/loading system? 0 Answers
Playerprefs to Save UI TEXT 2 Answers