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:
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.
asked Mar 16 '10 at 07:35 PM
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.
answered Mar 16 '10 at 08:09 PM
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:
answered Mar 17 '10 at 12:02 AM