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 Banana13 · Mar 09, 2014 at 10:53 AM · gameobjectfunctionloadleveldontdestroyonload

DontDestroyOnLoad does not work after reload the scene?

Hi everyone, I have a object that have a script with a DontDestroyOnLoad function. Now if I go from the scene 1 where the object is to scene 2 it is still there, but if I die in the scene 2 and reload the scene 2 its gone. How can I fix this that the object is still there after reload the scene 2?

Comment
Add comment · Show 1
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 wibble82 · Mar 09, 2014 at 11:12 AM 1
Share

Could you post the code where you: - load the scenes - call dontdestroyonload

In theory it should work fine (I do this all the time in my own code), so there's probably a small issue with your code somewhere that we'll need to see to help.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Banana13 · Mar 09, 2014 at 01:19 PM

Here is the code at the object that should not be destroyed:

 function Update () {
 DontDestroyOnLoad (gameObject);
 }

And this is my script where I reload the scene:

 using UnityEngine;
 using System.Collections;
 
 public class GameManager : MonoBehaviour {
     // The score that the player currently has
     [HideInInspector]
     public int curScore; 
     // The highest score the player has reached (saved)
     private int highscore;
     // Reference to our custom gui skin
     public GUISkin skin;
     // Values defining the width and height of our game over screen
     public Vector2 losePromptWH;
     // Boolean to check if we need to end the game or not
     [HideInInspector]
     public bool showGameOver;
 
     void Start () {
         // Grab the last saved highscore from the player prefs file
         highscore = PlayerPrefs.GetInt("Highscore");
     }
 
     void Update () {
         // If the bird died and our current score is greater than our saved highscore
         if (showGameOver && curScore > highscore)
         {
             // Set the highscore to our current score
             highscore = curScore;
             // Now save the score as our new highscore
             PlayerPrefs.SetInt("Highscore", highscore);
         }
     }
 
     void OnGUI()
     {
         // Set the GUI's skin to our custom skin
         GUI.skin = skin;
         // Show our current score value at the top center of the screen 
         // (note: it uses the custom Score style in our skin)
         GUI.Label (new Rect(Screen.width/2 - 100,100f,200,200), curScore.ToString(), skin.GetStyle("Score"));
 
         // If the bird died, show the game over screen
         if (showGameOver)
         {
             // Define the screen space for the game over window
             Rect currentGameOver = new Rect(Screen.width/2 - (losePromptWH.x/2), Screen.height/2 - (losePromptWH.y/2), losePromptWH.x, losePromptWH.y);
             // Generate a box based on the game over window rectangle
             GUI.Box (currentGameOver, "Game Over", skin.GetStyle("Game Over"));
             // Draw our current score within the game over window
             GUI.Label(new Rect(currentGameOver.x + 15f, currentGameOver.y + 250f, currentGameOver.width * .5f, currentGameOver.height * .25f), "Score: " + curScore.ToString());
             // Draw our highscore within the game over window (if the highscore was beaten, it will show your current score)
             GUI.Label(new Rect(currentGameOver.x + 150f, currentGameOver.y + 350f, currentGameOver.width * .5f, currentGameOver.height * .25f), "High score: " + highscore.ToString());
             // Draw a replay button on screen and check to see if it was clicked
             if (GUI.Button (new Rect(currentGameOver.x + (currentGameOver.width - 110), currentGameOver.y + (currentGameOver.height + 250), 330, 160), "", skin.GetStyle("Play")))
             {
                 // If it is clicked, reload the level
                 Application.LoadLevel("5 Level");
                 // Load the highscore from our save file
                 highscore = PlayerPrefs.GetInt("Highscore");
             }
 
             if (GUI.Button (new Rect(currentGameOver.x + (currentGameOver.width - 520), currentGameOver.y + (currentGameOver.height + 250), 330, 160), "", skin.GetStyle("back")))
             {
                 // If it is clicked, reload the level
                 Application.LoadLevel("1 menu");
 
             }
         }
     }
 }
Comment
Add comment · 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

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

22 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Method execution order after LoadLevel()? 1 Answer

overlap between scenes!!! 1 Answer

DontDestroyOnLoad - is it intended behavior? 3 Answers

Message sender...how to know who send the message 1 Answer


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