I have a Master object with a Master script on it, that has a :
function Awake () {
DontDestroyOnLoad(this);
}
attached to it, so it should stay through the game. The problem is it doesn’t. When a player dies, and chooses to retry, I just reload the level (easiest way to do it), but whenever I do the Master object is destroyed, or its memory is wiped. Either way, I cant save information in there for later use (like which checkpoint was last, and what upgrades have been gotten). Any help would be great.
How are you referring to your master object?
function Start() {
master = GameObject.Find(“Master”).GetComponent(“Master”);
if (master.checkPoint) {
transform.position = master.checkPoint.position;
transform.position.y += 5;
}
}
This is the code on load up of my player. It moves you right above the checkPoint, and lets you drop a little ways. It works the first time, but never the second. Even if I drag and drop in the checkPoint object, after the first death it doesnt work anymore. Here’s my reload code:
Application.LoadLevel(thisLevel);
thisLevel of course corresponding to a string with the level name stored on it.
I really dont know whats wrong.
You probably end up with multiple master objects when the level re-loads (as the first one is still there). You’re better off referring to a master/manager object with a static variable.
See http://www.unifycommunity.com/wiki/index.php?title=AManagerClass
Have you tried using the new LoadLevelAdditive?
Okay, so I put the Master object on another previous level now, so it wont load double when I reload that level. Its still not working. In face, my objects wont even reference its information.
PS
This worked for other things, so maybe I’m doing this part wrong:
function OnTriggerEnter (hit : Collider) {
if (hit.gameObject.tag == “Checkpoint”) {
Debug.Log(hit.gameObject.transform);
master.checkPoint = hit.gameObject.transform;
}
}
And in the Master object there is a transform variable called checkPoint.
If the object is actually being destroyed (versus you’re not finding the right one) you should be able to tell by looking at the hierarchy (it’s live while the game is running in the IDE). If there are multiple copies, you should also be able to tell.
If you have a script named Global_Stuff which has static variables and functions you should just be able to access it using:
Global_Stuff.foo
No need for FindComponent or anything like that.
If it’s DontDestroyOnLoad, you’re good to go.
“static” properties of a script are in essence “Class” properties, and classes are in global scope, so they’re rather handy. You just need to keep one instance around.
What Unity could use in addition to DontDestroyOnLoad is an “OnlyMakeOnceInstance” version. In a finished game, you could put the “master” object in a scene that only loads once, but for convenience during debugging, I have it in every scene, and set it up so new instances are disposed of (during Start() they check to see if the class is initialized; if it is, they kill themselves – oddly enough this is kind of “Singleton before OO programming was invented done upside down”…
If someone has a more elegant solution, I’d love to know it.