Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Whipexx · Aug 25, 2015 at 07:08 AM · destroyloadleveldontdestroyonload

Is there any better way to "reload the current level"?

I'm using a single Player object and a single Camera object for multiple levels of a game to be able to change seamlessly from one to another maintaining the settings and some variables (a countdown time is the most relevant).

To accomplish this I used the:

         DontDestroyOnLoad(transform.gameObject);

...on both the playerController and cameraController, so when the player dies and I reload the level I also need to manually destroy both instances so that they don't duplicate.

The problem comes when I die outside of the initial Level, because the objects get destroyed and don't spawn at the start of that level.

I'm thinking about doing a more elaborated restartGame function, so that I store the starting coordinates of all levels... but I'm also wondering if there is a better way to do all of this, and since I don't want to mess my code up too much I would like to know if any of you knows about a better way to do all this.

Greetings.

Comment
Add comment · Show 2
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 Ekta-Mehta-D · Aug 25, 2015 at 07:07 AM 0
Share

What kind of setting you want to maintain ? If you want maintain settings then save all those into player preference otherwise this will make your code complex.

avatar image Whipexx · Aug 25, 2015 at 09:36 AM 0
Share

By settings I meant mainly the player and camera position. I'm using a custom camera with a focus area and I wanted the leven changing to be as seamless as I could.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by GameVortex · Aug 25, 2015 at 07:12 AM

Instead of loading the player and camera in the first actual level you can use another Startup level which only has your player and camera and a script to load the first level. This way when reloading level one or any other level, you are not loading the Startup level and your player and camera will not be duplicated.

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
avatar image
1

Answer by NeverHopeless · Aug 25, 2015 at 07:45 AM

I would suggest you to establish a session (custom script using Singleton concept) and save your settings in this object. Don't use DontDestroyOnLoad since every scene load will produce at least a Camera., so you must have to destroy it if you want to keep the first camera only.

Lets first have a look at session object say GameSession:

 using System;
 
 public sealed class GameSession
 {
    private static volatile GameSession instance;
    private static object syncRoot = new Object();

    public int currentLevel;
    // declare you remaining variables here to maintain game settings.
    // possibly storing coordinates

    private GameSession() {}
 
    public static GameSession Instance
    {
       get 
       {
          if (instance == null) 
          {
             lock (syncRoot) 
             {
                if (instance == null) 
                   instance = new GameSession();
             }
          }
 
          return instance;
       }
    }
 }

you can use it like:

 GameSession.Instance.currentLevel = 1;

If you want to maintain this session in upcoming session, you mark this class serializable and store it in a file, and load file in the next session, which is a different story.

Now at this point you should write script in a way that you make just one Scene say GameScene and there setting will be retrieved from this session object described above. e.g.,

Script on camera:

 int currentLevel;
 GameObject enemyPrefab;
 public GameObject[] prefabCollection; // collection of enemy prefab to be instantiated on each level.
 
 void Start()
 {
        currentLevel  = GameSession.Instance.currentLevel;
       enemyPrefab =  Instantiate(prefabCollection[currentLevel]) as GameObject;     
 }

This way, on completion of first level you have to load you new level like this:

 void CustomGameCompletedEvent()
 {
      // Setup settings for new level
      GameSession.Instance.currentLevel = GameSession.Instance.currentLevel + 1;
 
      // Reload the same scene with different settings
      Application.LoadLevel("GameScene");
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to stop duplicates from dontdestroyonload 1 Answer

Delete duplicate gameobject on restart 1 Answer

InvokeRepeating on object with DontDestroyOnLoad 0 Answers

Animator destroyed on DontDestroyOnLoad 2 Answers

"Dont destroy on load" does not work at forst 2 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