• 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 PauMat27 · Jul 05, 2018 at 02:33 PM · scene-loadinggamedesign

Reload scene without reseting variables

I want to reload a scene using SceneManager.LoadScene (). However, by doing this, my LifeCount is setting to MaxLife once again, since I have

 void Start(){
 LifeCount=MaxLife;
 }

I need to mantain this line (you always start with MaxLife), however, when reloading the scene, your LifeCount must be decreased by a unit. Any idea? I've seen static variables, and doesn't work exactly how I want (maybe I don't use them properly)

Comment
Add comment
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

3 Replies

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

Answer by madks13 · Jul 05, 2018 at 02:43 PM

You need a GameManager. Also, using DontDestroyOnLoad.

Edit :

here is the example code for how to do this with a GameManager.

     using UnityEngine;
 
     //To be attached to an object inside the first scene
     public class GameManager : MonoBehaviour
     {
         private static bool _created = false;
 
         //Accessible only trough editor or from this class
         [SerializeField]
         private int maxLives = 5;
 
         public int livesLeft;
 
         private void Awake()
         {
             if (!_created)
             {
                 DontDestroyOnLoad(this.gameObject);
                 _created = true;
                 Init();
             }
         }
 
         public void Init()
         {
             livesLeft = maxLives;
         }
     }
 
     public class PlayerMove : MonoBehaviour
     {
         private GameManager _manager;
         private int _lives;
 
         private void Awake()
         {
             //The following line should work if you stick to having one GameManager in the game
             _manager = GameObject.FindObjectOfType<GameManager>();
             _lives = _manager.livesLeft;
         }
     }

You can then add features to your GameManager such as saving the lives left in the PlayerPrefs. This is basics in code architecture, separate responsibilities. Your game objects shouldn't all have to know how to save data from the scene, only one object has to do that, but the other objects need to have access to it. @PauMat27

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 PauMat27 · Jul 05, 2018 at 02:51 PM 0
Share

DontDestroyOnLoad on the Game$$anonymous$$anager script? Then I must communicate between two scripts (I was looking for something easier)

avatar image madks13 PauMat27 · Jul 05, 2018 at 02:59 PM 0
Share

Game$$anonymous$$anager should be a singleton, accessible from everywhere. And communication between scripts is a given in any program, let alone in a game. You won't be going far without it.

avatar image PauMat27 madks13 · Jul 05, 2018 at 05:03 PM 0
Share

Can you give a simple example? Here is my code

         using System.Collections; 
 using System.Collections.Generic; 
 using UnityEngine;
         
         public class Player$$anonymous$$ove: $$anonymous$$onoBehaviour {
             [HideInInspector] public int lifes=3;
              void OnTriggerEnter2D (Collider2D _other) { 
     if(_other.CompareTag("Enemy")){
     lifes--; 
     }
     }
     }

for the Player and I don't know how to program now the Game$$anonymous$$anager code. Thanks in advance and sorry for the indentaation, I don't know how it works on this editor.

avatar image
0

Answer by freakrho · Jul 05, 2018 at 02:43 PM

There are a couple of ways you can do this. You can either use DontDestroyOnLoad to make an object persistent between scenes or save the data on disk either using PlayerPrefs or serialization.

I prefer the first one, for that you just run the function DontDestroyOnLoad on Awake

 void Awake()
 {
     DontDestroyOnLoad(gameObject);
 }

For PlayerPrefs you can check the documentation on how to save and load data https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

Comment
Add comment · Show 2 · 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 PauMat27 · Jul 05, 2018 at 02:50 PM 0
Share

But I want to destroy the player, and with this, the player remains and each time I get another player.

avatar image freakrho PauMat27 · Jul 05, 2018 at 09:46 PM 0
Share

For that you need a dedicated object to store that kind of information. Singletons are an easy way to handle it.

avatar image
0

Answer by hameed-ullah-jan · Jul 05, 2018 at 06:06 PM

Hi, I think dontdestroyonload is a good solution for your problem, but you can also try "PlayerPrefs" in this scenario, PlayerPrefs are used for permanent storage of values. if you don't have knowledge of PlayerPrefs, try read this: https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

91 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 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 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 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

Classic Resident Evil-style room loading/level streaming? 4 Answers

How to have multiple scenes concurrently runnning 3 Answers

Load AssetBundles and External Scenes dynamically 0 Answers

Not loading in Chrome 0 Answers

Help with reloading scene for endless runner 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges