- Home /
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)
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
DontDestroyOnLoad on the Game$$anonymous$$anager script? Then I must communicate between two scripts (I was looking for something easier)
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.
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.
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
But I want to destroy the player, and with this, the player remains and each time I get another player.
For that you need a dedicated object to store that kind of information. Singletons are an easy way to handle it.
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