Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tea_Doogun · Jul 18, 2014 at 08:47 PM · dontdestroyonloadapplication.loadlevel

'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");
 }
 

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lo0NuhtiK · Jul 18, 2014 at 09:53 PM 1
Share

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.

avatar image rutter · Jul 18, 2014 at 10:00 PM 1
Share

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?

avatar image Tea_Doogun · Jul 18, 2014 at 10:40 PM 0
Share

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.

avatar image Tea_Doogun · Jul 18, 2014 at 10:42 PM 0
Share

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?

avatar image Lo0NuhtiK · Jul 18, 2014 at 10:47 PM 1
Share

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?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tea_Doogun · Jul 18, 2014 at 11:28 PM 1
Share

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 :)

avatar image Tea_Doogun · Jul 18, 2014 at 11:31 PM 0
Share

Thanks again :)

avatar image Lo0NuhtiK · Jul 18, 2014 at 11:31 PM 1
Share

Cool :) Glad it works for you now.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Application.LoadLevel object clone spam 0 Answers

DontDestroyOnLoad Missing Object 1 Answer

DontDestroyOnLoad calls Awake() and Start() function each time the scene is reloaded 4 Answers

DontDestroyOnLoad 0 Answers

Don't lose variable data when reloading a level - c# 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges