- Home /
Saving gameobject when switching scenes in javascript
I have heard so much about xml saving game objects when switching scenes, but i can not find any usefull tutorials how to do it with javascript, or is it possible at all. I would be gratefull if someone could just write part of code or leave me some link to see how to save simple game object with one sprite renderer, and one script attached.
Is it possible to somehow save game object as Prefab, and then instantiate it in new scene?
Answer by MolochSim · Oct 25, 2014 at 02:19 PM
In a script attached to the game object, include
DontDestroyOnLoad(gameObject);
in the Start() or Awake() methods. This way your object will persist if you load a different scene and it will contain all variables and changes made to it previously.
This might help you:
Answer by Ali-hatem · Oct 25, 2014 at 02:38 PM
//first attach this script to the game object and make your game object a prefab so you can add it to the other scenes and don't delete it from the scenes
public class Data : MonoBehaviour
{
public static Data dat; //this will make the class static so you can enter it without get component .
void Awake ()
{
if (dat == null)
{
DontDestroyOnLoad(gameObject);// will save the game object when change the level.
dat = this;
}
else if (dat != this)//will keep one saved object so no conflict .
{
Destroy(gameObject);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613165923im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I cant get my Xml save script to work. I dont know how. 2 Answers
XmlException: unexpected end of file. Current depth 1 Answer
Xml save/load script gives null reference exception in javascript 0 Answers
using System.xml to Parse XML in javascript 0 Answers
Declaring XML Namespace at Runtime 1 Answer