- Home /
How can I keep the game objects when I quit play mode?
Let's say I generated a few objects during runtime. Is there a way to prevent them being destroyed when I quit play mode and go back to edit mode?
it's not exactly what your after but there's an extremely handy plugin on the asset store called Play$$anonymous$$odePersist. It allows you to save out changes you made to components during play mode, so that they remain the same when you exit play mode. I don't know specifically what you want to do, but you could 'mock record' the values you need this way. Not ideal though...
An easy way to "keep" you gameObject is to copy them... Just select them before quitting play mode, press Ctrl+C, quit play mode then Ctrl+V
Thanks guys. I wish there were a way to do it programmatically but what are you gonna do? I guess we'll just have to improvise in situations like this.
Answer by fafase · Jun 16, 2014 at 03:25 PM
Here is a quick solution. Store the object with information you need on a file, just a basic text file or xml or json, whatever you want.
Then two ways, either update the file when you create a new object
In case 1, you could create a wrapper to the Instantiate method:
public static class Utility{
public static void Instantiate(GameObject prefab, Vector3 position, Quaternion rotation){
GameObject obj = (GameObject) Object.Instantiate(prefab,position, rotation);
// Here you add all the file access, writing and closing
}
}
}
or collect all objects in OnApplicationQuit which is called when you stop the play mode.
void OnApplicationQuit(){
GameObject[] allObjects = (GameObject[])FindObjectsOfTypeAll(typeof(GameObject));
foreach(object thisObject in allObjects){
// Access file, write and close
}
}
Then check this video http://unity3d.com/learn/tutorials/modules/intermediate/editor/adding-buttons-to-inspector
and create a button that does the revert effect, that is read the file and create the object stored on it.
[CustomEditor(typeof(ObjectBuilderScript))]
public class ObjectBuilderEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
ObjectBuilderScript myScript = (ObjectBuilderScript)target;
if(GUILayout.Button("Create objects"))
{
// Access file, read and create all objects
}
}
}
You are now with your scene in edit mode as it was in play mode.
+1, Simple and efficient considering you also took time for CustomEditor.
Great answer, I was wondering if Editor had something like this already, but it sounds messy and confusing. This solution might just be what I need, thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613151628im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to import the object from server to unity 2 Answers
Material doesn't have a color property '_Color' 4 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer