- Home /
Float var not decreasing when reload a scene
Hello guys.
In my game, has a start menu with a variable called refresh and it's value is decreased every frame while the scene is active, until this ok. In game, player can return to menu with pause, but when he returns the var refresh isn't being decreased, it is "locked" at inicial value. Why is this occurring? I am loading scene normally with SceneManager and decreasing in Update.
Thanks!
OBS: The void 'load' is the function called when player clicks on 'new game' button, the var 'clicou' is being true normally, but var refresh is not being decreased
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class TrocaDeCena : MonoBehaviour
{
private float refresh = 5f;
private bool clicou = false;
// Use this for initialization
void Start ()
{
refresh = 5f;
}
// Update is called once per frame
void Update()
{
if (clicou) {
refresh -= Time.deltaTime;
if (refresh <= 0) {
SceneManager.LoadScene ("general-ravana");
}
}
}
public void load()
{
if (clicou == false) {
clicou = true;
}
}
}
If you want help with an error in your code, you need to show us your code...
I didn't post the code because i was wondering that error was very usual, but I will post anyway, sorry
And when you return to the scene, is clicou true or false? Is load() being called when you click the button?
Answer by tanoshimi · Dec 20, 2016 at 10:54 PM
There's nothing wrong with your code, and your additional comments suggest every variable has the right value and methods are called correctly. However, one detail in your description alerted me: "player can return to menu with pause, but when he returns the var refresh isn't being decreased".
Based on this, I'm going to hazard a guess... do you set Time.timeScale = 0
in the pause menu before reloading this scene? If so, Update() functions are no longer going to be refreshed. If I'm correct, you simply need to restore the correct timescale before reloading the scene.
Yeah, definitely the pause was the problem, thank you so much! I didn't know if i change time scale in a scene, it will be changed in another scene too. Am I correct?
Answer by vithorescames · Dec 21, 2016 at 07:34 AM
Yeah, definitely the pause was the problem, thank you so much! I didn't know if i change time scale in a scene, it will be changed in another scene too. Am I correct?