- Home /
'DontDestroyOnLoad' is destroying other gameobjects
HI :)
I have a game manager (empty gameobject) which i want to go from level to level as my player progresses.
On the game manager, is a score keeping script (in java script), within the 'Awake' function is the line 'DontDestroyOnLoad(transform.gameObject);'.
Here's the problem: When i load a new level using: Application.LoadLevel ("Star Counter"); The gameobjects in the new scene are displayed as 'deleted gameobject' and can't be selected
The camera is still showing the scene (but is listed as a 'deleted gameobject')
The background is still shown, but the texture isn't scrolling (as i programmed it to do...and is listed as a 'deleted gameobject')
If i remove the DontDestroyOnLoad line from the game manager, the gameobjects in the new level are fine.
Why is this happenening, how can it be fixed?
Thank you as always for your time, Tom :)
(EDIT) The game manager holds the scores (collected stars) and the GUI for the game and loads the next level when needed.
Here's the script:
var zoe : GameObject;
var kim : GameObject;
// Star counting: ///////////////////////////////////////
var zoeStarsYellow : int;
var kimStarsYellow : int;
var zoeStarsBlue : int;
var kimStarsBlue : int;
var zoeStarsGreen : int;
var kimStarsGreen : int;
var zoeStarsRed : int;
var kimStarsRed : int;
var zoeStarsCon1 : int;
var kimStarsCon1 : int;
var totalYellowStars : int;
var totalBlueStars : int;
var totalGreenStars : int;
var totalRedStars : int;
/////////////////////////////////////////////////////////
var seesaw : GameObject;
// GUI Junk: ////////////////////////////////////////////
var bWidth1 : float = 10; //size of game GUI boxes
var bHeight1 : float = 15;
var sWidth1 : float = 10; //position of game GUI Textures
var sHeight1 : float = 10;
var sWidth2 : float = 10;
var sHeight2 : float = 10;
var sWidth3 : float = 10;
var sHeight3 : float = 10;
var sWidth4 : float = 10;
var sHeight4 : float = 10;
var sWidth5 : float = 10;
var sHeight5 : float = 10;
var enableGameGUI : boolean = true;
var enableCounterGUI : boolean = false;
var bWidth2 : float = 10; //size of counter GUI boxes
var bHeight2 : float = 15;
var yellowWidth1 : float = 10; //position of counter GUI Textures
var yellowHeight1 : float = 10;
var blueWidth2 : float = 10;
var blueHeight2 : float = 10;
var greenWidth3 : float = 10;
var greenHeight3 : float = 10;
var redWidth4 : float = 10;
var redHeight4 : float = 10;
var yellowStarsNeeded : int;
var blueStarsNeeded : int;
var greenStarsNeeded : int;
var redStarsNeeded : int;
var jumpsLeft : int;
var maxJumps : int;
var gooeyTextYellow : GUIStyle;
var gooeyTextBlue : GUIStyle;
var gooeyTextGreen : GUIStyle;
var gooeyTextRed : GUIStyle;
var gooeyJumpsLeft : GUIStyle;
var gooeyTextYellow2 : GUIStyle;
var gooeyTextBlue2 : GUIStyle;
var gooeyTextGreen2 : GUIStyle;
var gooeyTextRed2 : GUIStyle;
var gooeyTick : GUIStyle;
var gooeyCross : GUIStyle;
var yellowSuccess : boolean;
var blueSuccess : boolean;
var greenSuccess : boolean;
var redSuccess : boolean;
/////////////////////////////////////////////////////////
function Awake () {
DontDestroyOnLoad(transform.gameObject);
seesaw = gameObject.Find("JumpManager");
zoe = gameObject.Find("P1");
kim = gameObject.Find("P2");
jumpsLeft = -2;
maxJumps = 2;
if (Application.loadedLevelName == "sceneTest")
{
enableGameGUI = true;
enableCounterGUI = false;
totalYellowStars = 0;
totalBlueStars = 0;
totalGreenStars = 0;
totalRedStars = 0;
yellowStarsNeeded = 100;
blueStarsNeeded = 100;
greenStarsNeeded = 100;
redStarsNeeded = 100;
}
if (Application.loadedLevelName == "Star Counter")
{
enableGameGUI = false;
enableCounterGUI = true;
}
}
function Update () {
totalYellowStars = (zoeStarsYellow + kimStarsYellow);
totalBlueStars = (zoeStarsBlue + kimStarsBlue);
totalGreenStars = (zoeStarsGreen + kimStarsGreen);
totalRedStars = (zoeStarsRed + kimStarsRed);
if (jumpsLeft >= maxJumps)
{
loadStarCounter ();
//Application.LoadLevel ("Star Counter");
print ("End Of Level");
}
if (enableCounterGUI == true)
{
if (totalYellowStars >= yellowStarsNeeded)
{
yellowSuccess = true;
}
else
{
yellowSucess = false;
}
}
}
function OnGUI () {
if (enableGameGUI == true)
{
GUI.Label (Rect (Screen.width/sWidth1, Screen.height/sHeight1, Screen.height/bHeight1, Screen.height/bHeight1), totalYellowStars.ToString() + "/" + yellowStarsNeeded.ToString(), gooeyTextYellow);
GUI.Label (Rect (Screen.width/sWidth2, Screen.height/sHeight2, Screen.height/bHeight1, Screen.height/bHeight1), totalBlueStars.ToString() + "/" + blueStarsNeeded, gooeyTextBlue);
GUI.Label (Rect (Screen.width/sWidth3, Screen.height/sHeight3, Screen.height/bHeight1, Screen.height/bHeight1), totalGreenStars.ToString() + "/" + greenStarsNeeded, gooeyTextGreen);
GUI.Label (Rect (Screen.width/sWidth4, Screen.height/sHeight4, Screen.height/bHeight1, Screen.height/bHeight1), totalRedStars.ToString() + "/" + redStarsNeeded, gooeyTextRed);
GUI.Label (Rect (Screen.width/sWidth5, Screen.height/sHeight5, Screen.height/bHeight1, Screen.height/bHeight1), jumpsLeft.ToString() + "/" + maxJumps.ToString(), gooeyJumpsLeft);
}
if (enableCounterGUI == true)
{
GUI.Label (Rect (Screen.width/yellowWidth1, Screen.height/yellowHeight1, Screen.height/bHeight1, Screen.height/bHeight1), "Yellow Stars: " + totalYellowStars.ToString() + "/" + yellowStarsNeeded.ToString(), gooeyTextYellow2);
GUI.Label (Rect (Screen.width/blueWidth2, Screen.height/blueHeight2, Screen.height/bHeight1, Screen.height/bHeight1), "Blue Stars: " + totalBlueStars.ToString() + "/" + blueStarsNeeded, gooeyTextBlue2);
GUI.Label (Rect (Screen.width/greenWidth3, Screen.height/greenHeight3, Screen.height/bHeight1, Screen.height/bHeight1), "Green Stars: " + totalGreenStars.ToString() + "/" + greenStarsNeeded, gooeyTextGreen2);
GUI.Label (Rect (Screen.width/redWidth4, Screen.height/redHeight4, Screen.height/bHeight1, Screen.height/bHeight1), "Red Stars: " + totalRedStars.ToString() + "/" + redStarsNeeded, gooeyTextRed2);
if (yellowSuccess == true)
{
GUI.Label (Rect (Screen.width/yellowWidth1, Screen.height/yellowHeight1, Screen.height/bHeight1, Screen.height/bHeight1), " " ,gooeyTick);
}
else
{
GUI.Label (Rect (Screen.width/yellowWidth1, Screen.height/yellowHeight1, Screen.height/bHeight1, Screen.height/bHeight1), " ", gooeyCross);
}
}
}
function loadStarCounter ()
{
enableGameGUI = false;
enableCounterGUI = true;
Application.LoadLevel ("Star Counter");
}
If you remove that line from it, that also means that that game manager thing isn't living into the next level when it loads. Something else is going on with that game manager when it lives into the next level.
Are these other components all attached to the same GameObject as your game manager? Depending on your setup, it's possible that the manager survives but has references to other objects that have been destroyed.
If your loaded scene has a duplicate game manager, or some other duplicate object, how do you handle that?
Hi Rutter, the deleted gameobjects are the objects which are loaded into the new scene which I put there manually when setting up the scene. The game manager isn't parented to anything.
There's no other game manager object in the new scene, just the one that gets transfered from the previous level.
Hey Loon, that's right. Removing the line stops the manger being transferred but also stops the new scenes gameobjects being deleted. Any idea what might cause that?
That's what I'm saying... There's something else going on with your game manager that we have no info about because all we know of your game manager is that it screws stuff up when it lives into the next level. DontDestroyOnLoad isn't causing the problem, its something else your living game manager is doing/affecting afterward. ... what else are you doing with it?
Answer by Lo0NuhtiK · Jul 18, 2014 at 11:21 PM
Does [jumpsLeft] ever get reset to zero or below maxJumps somewhere?? Awake() is only called the very first time this script comes into existence. If you're not resetting jumpsLeft to something < maxJumps somewhere, then it looks like as soon as jumpsLeft is >= maxJumps then every Update you're going to just keep calling the loadStartCounter() function and loading Star Counter scene, then it starts, then in update you load it again, and so on.
Holy frag, that did it. Added a line in 'function loadStarCounter':
function loadStarCounter ()
{
enableGameGUI = false;
enableCounterGUI = true;
jumpsLeft = -2; // <--------ADDED THIS LINE, thanks Loon :)
Application.LoadLevel ("Star Counter");
}
Thanks man...add your comment as an answer so i can give you proper credit :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613154435im_/https://answers.unity.com/themes/thub/images/avi.jpg)