x


DontDestroyOnLoad() does not seem to be working.

I have an empty game object with a script attached to it which I intend to use to keep track of variables between level loads. I am using it to keep track of checkpoints. When playing the checkpoint is updated in the gameObject, but when the level is loaded the variables I changed are reset. Here is my code:

var CurrentSpawn : GameObject;

function Start() {
    CurrentSpawn.SendMessage("SpawnCharacter");
}

function Awake() {
    DontDestroyOnLoad(this);
}

function Update() {
    print(CurrentSpawn.transform.name);
}

A different function changes the spawn point and that part is working, it is just that the spawn point is reset to the original when the level is re-loaded.

Any assistance would greatly be appreciated.

more ▼

asked Mar 16 '10 at 07:35 PM

bublebboy gravatar image

bublebboy
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

That's probably because "CurrentSpawn" references a different object which is destroyed on load. When the new re-set version of the level comes around, that same spawnpoint gameobject is probably considered - by Unity - to be an entirely different new object.

Perhaps instead of storing a reference to that object, you should just store the position of the spawnpoint as a Vector3 (and also the rotation as a Quaternion, if that is required). Because Vector3's and Quaternions are structs, your script will retain its own copy of the values, rather than referencing some other object which gets flushed away when the Load occurs.

more ▼

answered Mar 16 '10 at 08:09 PM

duck gravatar image

duck ♦♦
40.9k 92 148 415

Thank you very much for the information! I was able to use it to get my checkpoints working!

Mar 16 '10 at 11:16 PM bublebboy
(comments are locked)
10|3000 characters needed characters left

If you want to keep CurrentSpawn alive as well, you need to tell it not to destroy as well, by the way, to differentiate between vars and functions and objects its practice to start a variable with lowercase:

var currentSpawn : GameObject;

function Start() {
    currentSpawn.SendMessage("SpawnCharacter");
}

function Awake() {
    DontDestroyOnLoad(this);
    DontDestroyOnLoad(currentSpawn);
}

function Update() {
    print(currentSpawn.transform.name);
}
more ▼

answered Mar 17 '10 at 12:02 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.4k 20 26 70

(comments are locked)
10|3000 characters needed characters left

If an example would help, I posted an Answer in another Question:
How to Keep Information when Reloading a Level.
Which basically points to a demo package I wrote, with code, at:
Simple Demo of a Level Manager

more ▼

answered Mar 16 '10 at 08:28 PM

Cyclops gravatar image

Cyclops
7.1k 33 63 115

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x110
x82

asked: Mar 16 '10 at 07:35 PM

Seen: 3946 times

Last Updated: Mar 16 '10 at 07:35 PM