- Home /
After scene reload script stopped working.
I'm using this SceneManager.LoadScene(SceneManager.GetActiveScene ().buildIndex);
for scene reload and after reload this script stopped working
void Update (){
if (isDead == false) {
CheckUserInput ();
}
}
void CheckUserInput ()
{
if (Input.GetKeyDown (KeyCode.A)) {
transform.position += new Vector3 (-1, 0, 0);
}
if (Input.GetKeyDown (KeyCode.D)) {
transform.position += new Vector3 (1, 0, 0);
}
if (Input.GetKeyDown (KeyCode.W)) {
transform.position += new Vector3 (0, 1, 0);
}
if (Input.GetKeyDown (KeyCode.S)) {
transform.position += new Vector3 (0, -1, 0);
}
if (Mathf.Round(Time.time * 3) - speed == moveSpeed) {
transform.position += new Vector3 (0, 1, 0);
speed = Mathf.Round(Time.time * 3); ///////////AFTER scene reload time "stopped working"
Controls (WASD) still work correctly.
Answer by leSamo · Jan 03, 2018 at 11:00 AM
It depends on what is your script attached to, but usually reloading scene will destroy your script and create a new instance. If you want a variable to persist on scene reload use either static variables or use https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Your answer
Follow this Question
Related Questions
Help with time.time when new scene starts. 1 Answer
Reset timeSinceLevelLoad? 0 Answers
How to check if input was made x seconds/frames ago? 0 Answers
Time based scoring 2 Answers
Unity seems to be taking a long time destroying a scene? 0 Answers