Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Dont Destroy on Load not doing its job

Discussion in 'Scripting' started by gizzerd91, Aug 23, 2008.

  1. gizzerd91

    gizzerd91

    Joined:
    May 4, 2008
    Posts:
    311
    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.
     
  2. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    How are you referring to your master object?
     
  3. gizzerd91

    gizzerd91

    Joined:
    May 4, 2008
    Posts:
    311
    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.
     
  4. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
  5. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    Have you tried using the new LoadLevelAdditive?
     
  6. gizzerd91

    gizzerd91

    Joined:
    May 4, 2008
    Posts:
    311
    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.
     
  7. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    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.