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
1
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
2
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.

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

same script for two scenes, how can i call a function for particular scene with out affecting that function for another scene?? 0 Answers

how to fix missing reference exception when reload scene 1 Answer

Merge Scenes or not (runtime) 0 Answers

InvalidOperationException: This can only be used during play mode, please use EditorSceneManager.OpenScene() instead. 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