- Home /
Keeping position of game objects when leaving play mode
I use Unity to create simulations to move objects in a 3D world. I'd like to keep the position of the moved objects when leaving the play mode to use this position in other softwares.
I found a lot of questions about this, explaining how to use the copy/paste function which is really a good way, but if I forgot to copy, I loose everything ... And if I have a lot of objects, it's not so convenient.
I also found a KeepMeAlive script, using the instantiate function which works pretty well. I can get the objects with this script associated to at the end of the simulation, but they are visible in the scene windows and not attached in the Hierarchy. I still have to copy/paste them to get them as reusable gameobjects. Here is this script:
public class KeepMeAlive : MonoBehaviour {
void OnDestroy()
{
if (Application.isPlaying)
Instantiate(this, this.transform.position, this.transform.rotation);
}
}
So I tried to update it to choose where the clone is instantiated, using the saved Folder as a gameobject define before the simulation :
public class KeepMeAlive : MonoBehaviour {
public GameObject SaveFolder;
GameObject mySavedTruc;
public GameObject myOriginalTruc;
void OnDestroy()
{
if (Application.isPlaying)
mySavedTruc = Instantiate(myOriginalTruc, myOriginalTruc.transform.position, myOriginalTruc.transform.rotation);
mySavedTruc.transform.parent=SaveFolder.transform;
}
}
I still get my gameobject in the scene but not in the Hierarchy. And I get this error message : "Cannot set the parent of the GameObject 'vitre(Clone)(Clone)' while its new parent 'SauvegardeObjetsMobiles' is being destroyed"
Is it possible to get these instantiated gameobjects in the Hierarchy ?