Hi there,
I have three scenes at the following indexes
0 - Main Menu: Features a “New Game” button and a “Quit” button.
1 - Loading: Progress bar with async operation to load the third scene
2 - Game: Procedural city generator
The first time I go through the process of clicking “New Game”, the loading screen works really nicely. If I then return to the main menu using the in-game pause menu and click “New Game” again, the loading screen appears but nothing else happens.
I have absolutely no idea what to do, and although other people have had this issue, I cannot get any of the suggested fixes to work.
Here’s my code for the scene loading class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class SceneLoading : MonoBehaviour
{
[SerializeField] private Image progressBar;
// Start is called before the first frame update
void Start()
{
StartCoroutine(DelayLoading());
}
IEnumerator DelayLoading()
{
yield return new WaitForSeconds(1.0f);
StartCoroutine(LoadGameScene());
}
IEnumerator LoadGameScene()
{
yield return null;
// Begin scene loading
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(2);
// Prevent automatic scene activation
asyncOperation.allowSceneActivation = false;
// Whilst loading, update debug output and progress bar
while (!asyncOperation.isDone)
{
// Output the progress to the console
Debug.Log("[SceneLoading] Loading: " + (asyncOperation.progress * 100) + "%");
// Progress bar fill == async operation
progressBar.fillAmount = asyncOperation.progress;
// Check if the loading has finished
if (asyncOperation.progress >= 0.9f)
{
// Enable automatic scene activation
asyncOperation.allowSceneActivation = true;
}
yield return null;
}
}
}