So if you want to keep an object or script that keeps up variables (or for any other reason) when you go from scene to scene, you need to attach a don't destroy on load static function, which goes something like this:
function Awake () {
DontDestroyOnLoad (transform.gameObject);
}
function Awake () {
DontDestroyOnLoad (transform.gameObject);
}
The Awake function is call only once, when all the objects in the scene have been created. Read more about it here.
DontDestroyOnLoad has what ever is in the ( ) to not be destroyed when creating a new scene.
(transform.gameObject) is what will not be destroyed when the new scene is loaded, in this case, it will be the game object and all it's children the script is attached to.
Now that the object is not destroyed, when ever you load the scene that has the object again, that same object will be created. This usually isn't what people want. If you are one of those people, your going to need to create a script to check if the object has been created, and if it hasn't create the object. One way is:
So just attach that to some game Object in your level. =D
DontDestroyOnLoad has what ever is in the ( ) to not be destroyed when creating a new scene.
(transform.gameObject) is what will not be destroyed when the new scene is loaded, in this case, it will be the game object and all it's children the script is attached to.
Now that the object is not destroyed, when ever you load the scene that has the object again, that same object will be created. This usually isn't what people want. If you are one of those people, your going to need to create a script to check if the object has been created, and if it hasn't create the object. One way is:
static var stats = null;
var objectToBeCreated:Transform;
public class GameStatus {
function GameStatus() {
}
}
function Awake () {
if(stats == null){
Debug.Log("stats == null");
stats = new GameStatus();
Instantiate(objectToBeCreated);
}
}
So just attach that to some game Object in your level. =D
The problem "Now that the object is not destroyed, when ever you load the scene that has the object again, that same object will be created. This usually isn't what people want." is exactly what I want to fixed. Your solution does not inherit from monobehaviour and without that you cannot use Awake () (it will not be called automatically). If you inherit from monobehaviour, you cannot do "stats = new GameStatus()"
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteFixed it though,
ReplyDeleteI created an Empty GameObject that holds a reference to a prefab of the object I want to create and live over scenes and when the scene it was created from is loaded again, I check if the object exists first before deciding to instantiate another copy or not.
-------------------------
public GameObject my_Prefab_Instance;
void Awake(){
if (!GameObject.Find ("Prefab(Clone)")){
Instantiate(my_Prefab_Instance);
}
}
can u help me, explain to me by mail more detail about using this ?? I'm newbie who want to try this code on loadscene, thanks
ReplyDelete