Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Start Game - strange Bug

Discussion in 'Scripting' started by Xurium-, Jan 5, 2020.

  1. Xurium-

    Xurium-

    Joined:
    Jan 1, 2020
    Posts:
    4
    Hey guys, greetings from Germany ;)

    Well, I made myself a MainMenu, if you click Start, it switches to a Scene called "LoadingScreen" , there it loads the Scene "Game" and shows the progress in Bar and Text, then the "Game" Scene starts. In there I got a Pause Menu, activated and closed with ESC, where you can get through clicking a button to the MainMenu.

    And here is the Problem: Back in the MainMenu clicking Start again, nothing happens. The button is marked as selected, but it won't start the loading screen again...and I just don't get why...

    This is the code used in the MainMenu:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class MainMenu : MonoBehaviour
    7. {
    8.  
    9.     public void PlayGame ()
    10.     {
    11.         Invoke("LoadScreen", 1.0f);
    12.     }
    13.  
    14.     void LoadScreen()
    15.     {
    16.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    17.     }
    18.  
    19.     public void QuitGame ()
    20.     {
    21.         Debug.Log("QUIT!");
    22.         Application.Quit();
    23.     }
    24.  
    25.     public void LoadGame ()
    26.     {
    27.         Debug.Log("LoadSavegame");
    28.     }
    29. }
    This is the code used in the loading screen:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class LoadingScreen : MonoBehaviour
    8. {
    9.     public Text ProzentText;
    10.     [SerializeField]
    11.     private Image _ProgressBar;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         //start scene loading in Background
    17.         Invoke("StartGameScene", 1.0f);
    18.     }
    19.  
    20.     void StartGameScene()
    21.         {
    22.         StartCoroutine(LoadAsyncOperation());
    23.         }
    24.  
    25.     IEnumerator LoadAsyncOperation()
    26.     {
    27.         //create loading operation
    28.         AsyncOperation gameLevel = SceneManager.LoadSceneAsync(2);
    29.  
    30.         while (gameLevel.progress < 1)
    31.         {
    32.             //fill bar sync to loading progress
    33.             float progress = Mathf.Clamp01(gameLevel.progress / .9f);
    34.             _ProgressBar.fillAmount = progress;
    35.             ProzentText.text = (progress * 100).ToString("F0") + "%";
    36.             yield return new WaitForEndOfFrame();
    37.         }
    38.     }
    39. }
    This the Code of the PauseMenu (just in case):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityStandardAssets.Characters.FirstPerson;
    6.  
    7. public class PauseMenu : MonoBehaviour
    8. {
    9.     public static bool GameIsPaused = false;
    10.  
    11.     public GameObject PauseMenuUI;
    12.  
    13.  
    14.     void Start()
    15.     {
    16.         PauseMenuUI.SetActive(false);
    17.         Time.timeScale = 1f;
    18.         GameIsPaused = false;
    19.  
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update() {
    24.         if (Input.GetKeyDown(KeyCode.Escape))
    25.         {
    26.             if (GameIsPaused)
    27.             {
    28.                 Resume();
    29.             } else
    30.             {
    31.                 Pause();
    32.             }
    33.         }
    34.     }
    35.  
    36.     public void Resume()
    37.     {
    38.         PauseMenuUI.SetActive(false);
    39.         Time.timeScale = 1f;
    40.         GameIsPaused = false;
    41.     }
    42.  
    43.     public void Pause()
    44.     {
    45.         PauseMenuUI.SetActive(true);
    46.         Time.timeScale = 0f;
    47.         GameIsPaused = true;
    48.     }
    49.  
    50.     public void LoadMenu()
    51.     {
    52.         SceneManager.LoadScene("Menu");
    53.     }
    54.  
    55.     public void QuitGame()
    56.     {
    57.         Debug.Log("Quiting Game...");
    58.         Application.Quit();
    59.     }
    60.  
    61.     public void SaveGame()
    62.     {
    63.         Debug.Log("SavingMenu");
    64.     }
    65.  
    66.     public void LoadGame()
    67.     {
    68.         Debug.Log("LoadingMenu");
    69.     }
    70. }
    The Scene-Nr. of my Scenes are Menu = 0, LoadingsScreen = 1, Game = 2

    Please help me, I just don't get it... ^^
    Cheers,
    Xurium
     
    Last edited: Jan 5, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    First script, line 16, why don't you remove all that "current scene + 1" stuff and explicitly load the scene number you want for now, see if that works. If it works, return the code to this way and print the numbers.
     
  3. Xurium-

    Xurium-

    Joined:
    Jan 1, 2020
    Posts:
    4
    Hey there,
    thanks. Unfortunally I tried that already, it is now changed to SceneManager.LoadScene(2); , but the problem still remains.
    The button works fine when clicking it the first Time and then doesn't work anymore, when you return to the MainMenu from the Game.
    It worked fine, before I implimented the LoadingScreen, but I can't find a reason in there for my problem...
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Hm, it certainly looks okay, but you can drastically simplify your loading script. Did you know
    Start()
    can actually be an
    IEnumerator
    and Unity will treat it as a coroutine? Your code would become:

    Code (csharp):
    1. IEnumerator Start()
    2. {
    3.   yield return new WaitForSeconds(1.0f); // your former invoke delay
    4.  
    5. // now copy/paste the blurb that is presently inside the
    6. // IEnumerator LoadAsyncOperation() function!
    7.  
    8. }
    That will remove a lot of moving parts.
     
  5. Xurium-

    Xurium-

    Joined:
    Jan 1, 2020
    Posts:
    4
    You mean like this?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class LoadingScreen : MonoBehaviour
    8. {
    9.     public Text ProzentText;
    10.     [SerializeField]
    11.     private Image _ProgressBar;
    12.  
    13.     IEnumerator Start()
    14.     {
    15.         yield return new WaitForSeconds(1.0f);
    16.         //create loading operation
    17.         AsyncOperation gameLevel = SceneManager.LoadSceneAsync(3);
    18.  
    19.         while (gameLevel.progress < 1)
    20.         {
    21.             //fill bar sync to loading progress
    22.             float progress = Mathf.Clamp01(gameLevel.progress / .9f);
    23.             _ProgressBar.fillAmount = progress;
    24.             ProzentText.text = progress * 100 + "%";
    25.             yield return new WaitForEndOfFrame();
    26.         }
    27.     }
    28. }
    Unfortunally it didn't fix my Menu-Problem...
    Thanks so far.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Well yes, but looks like you've changed (2) to (3)...

    In other simplifications, why don't you use the string version of LoadSceneAsync? If you just use the string, you don't need to worry about constantly renumbering every time you add new scenes, or doing whacky math to try and figure out where to go next. I generally have a static class with all the scene names in it, and I think there's a Unity wiki script to produce such a static class based on what it finds in your Editor Build Settings...

    Code (csharp):
    1. const string s_MainMenu = "mainmenu";
    And then just use that constant everywhere...