My game has multiple levels, and also gives the player the ability to re-load the level they’re currently on (restart).
To restart the level, I simply trigger:
Application.LoadLevel ( LevelLoaded ); //LevelLoaded is set to the current level number when the game boots
The problem is that when the level “reloads”, Unity is complaing that I’m “still trying to access even though they’ve been destroyed”.
How can this be? I was under the impression that when you used LoadLevel, the only things that remained were objects you’d marked with “dontdestroyonload”?
Hmmm… definitely something wierd going on. I only have one object I say not to delete - the object which contains player scores and the like. This is in a GameObject of it’s own, so shouldn’t affect anything else. When I “reboot” the level, the new scene has TWO of these objects in it, even though it isn’t generated by script - it’s present in the heirarchy of the game level.
On top of this, it looks like some of my scripts are still executing! I use Brady’s Sprite Manager 2, and it keeps kicking out an error saying I’m trying to access a Transform that’s been destroyed - even though the very fact that I’ve reloaded the level should have reset everything?
The only way I can get the level to reload is to load a blank scene with a script which reloads the level that called it. So effectively the logic goes Level → Blank “Reloader” → Level
DontDestroyOnLoad() preserves the GameObject that the calling script is attached to, so if there’s any other scripts on that GameObject, they will stick around as well.
If you want to ensure you always have one and only one instance of the GameObject/script, use something like this:
using UnityEngine;
using System.Collections;
public class Singleton : MonoBehaviour
{
static Singleton instance = null;
void Start ()
{
if (instance != null)
{
Destroy(gameObject);
return;
}
DontDestroyOnLoad(gameObject);
instance = this;
}
}
Yeah - that’s not really a problem - the key issue is that the whole thing doesn’t seem to be functioning correctly - scripts are still running, objects aren’t being deleted, and the only object which isn’t supposed to be deleted is being duplicated!
There’s only one “DontDestroyOnLoad” in my entire project, and the content of it is minimal:
public class CoreGame: MonoBehaviour {
public enum GameLevel {
Level1
}
public int PlayerScore;
public GameLevel CurrentLevel;
void Start ()
{
DontDestroyOnLoad (gameObject);
PlayerScore = 0;
CurrentLevel = GameLevel.Level1;
}
}
Obviously I must be doing something wrong - the way I’m having to reload the level at the moment is ridiculous! :-S
Check the script on the object set to not destroy. Does it refer to a transform that has been destroyed in the LoadLevel process?
It appears from the error message that it isn’t a case of objects not being deleted, it is a case of they are being deleted, but something is still trying to access them. For example, you refer to GameLevel.Level. Is GameLevel accessed again after void Start? Is the GameLevel script attached to an object that has been destroyed?
Nope - the script you see in the code above is everything that is in the DontDestroyOnLoad object, with only the names changed. No transforms referred to. The error system actually points to a file which references the DontDestroyOnLoad object, but surely that wouldn’t cause the problem?
Can you post the script that references the DontDestroyOnLoad object (or at least the bits that are throwing up the errors)? Maybe it is referring to a missing transform in a subtle way.
I have this exact problem. Did you find an answer? I am also using SM2, and it is a gameobject with a packed sprite on it that won’t leave memory when I reload, even I am using no DontDestroyOnLoad calls whatsoever.