The question is answered, right answer was accepted
mainmenuscript keeps crashing the game
So i have made this mainmenu-script which works perfectly fine, if u start in mainmenu and then load a lv. Going from any lv to mainmenu works fine too. But if go from a lv to mainmenu and then try to access any lv the game crashes. I tried modifying it a bit, but that made it worse, so i reset to a way i know it works at least a bit. Can someone tell what's causing the bug?
Maybe some additional bug behaivour note, once the bug is caused and everthing frozen, u can simply, pause and unpause to get a gameover screen, which reloads same lv and suddenly its fine.
Here the code i wrote:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class MainmenuManager : MonoBehaviour {
//CanvasGroup HUDCanvasgroup;
// Use this for initialization
void Start () {
//GameObject HUDCanvas = GameObject.Find ("HUDCanvas");
//HUDCanvasgroup = HUDCanvas.GetComponent<CanvasGroup> ();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyUp (KeyCode.Escape)) {
quit();
}
}
// loads the lv with number lv
public void loadLevel(int lv){
SceneManager.LoadScene (lv);
}
//ends the programm
public void quit(){
Application.Quit();
}
public void gotoMainmenu(){
SceneManager.LoadScene ("Mainmenu");
}
}
Select all of the code and press the "101010" button, it's inbetween the Blockquote and the Attachment button. Also paste the error that you are encountering, or is it crashing and closing Unity without outputting an error?
Thx, that looks already nicer. Nah just the game window frezes and no error output when that happens. To add to music and rest is not affected by the gamefreeze. I do get sometimes a "Error adding System: Data not avaiable" though no link to a script nor a gameobject, so not really helpful.
oh and i managed to "fix" as far that the "pause trick" doesnt cause a instant game over but let u play right away without reloading the scene.
I don't see any problems with the script. There must be something else going on in the scene. Do you have scripts that have DontDestroyOnLoad? Unity shouldn't crash so easily, it's a strange problem here.
ok, fixed it. Thx for the help finally i can finish this project for good :-D Yep it was something that doesnt DestroyOnLoad but not a gameObject but the variable: time.TimeScale which seems to be a global instance and not diffrent for every lv. (weird) It's not indicated (hinted at best) that its influences all scenes in the Scripting API.
Pause turned timeScale to 0. So now i turn it to 1 before loading the lv.
Is there a list of all build in global variables which DON'T change, when loading a new scene?