GameManager Problem with loading scene

Hi, I’m a beginner in Unity and I want to make a loading scene for my game but I think my GameManager is awful.
But when I load my scene the canvas of the loading scene doesn’t appear.

If you can help me :slight_smile:

using System.Collections;
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
// Game States
public enum GameState { INTRO, MAIN_MENU, PAUSED, GAME, CREDITS, HELP }

public delegate void OnStateChangeHandler();

public class SimpleGameManager: Singleton
{
public SimpleGameManager() {}
private static SimpleGameManager instance = null;
public event OnStateChangeHandler OnStateChange;
public GameState gameState { get; private set; }

public GameObject[] SystemPrefabs;
private List<GameObject> _instanciedSystemPrefabs;

AsyncOperation operation;
public GameObject loadingScreen;
public Text progressText;
public Slider slider;

List<AsyncOperation> _loadOperations;

private void Start()
{
    _loadOperations = new List<AsyncOperation>();
    _instanciedSystemPrefabs = new List<GameObject>();
    InstantiateSystemPrefabs();
    //if (GameObject.FindGameObjectWithTag("EcranDeChargement") != null)
    //{
    //    loadingScreen = GameObject.FindGameObjectWithTag("EcranDeChargement");
    //    progressText = GameObject.FindGameObjectWithTag("progressText").GetComponent<Text>();
    //    slider = GameObject.FindGameObjectWithTag("Slider").GetComponent<Slider>();
    //    loadingScreen.SetActive(false);
    //    Debug.Log("1er");
    //}
    //Debug.Log("2eme");
    InstantiateSystemPrefabs();
    DontDestroyOnLoad(gameObject);
    //DontDestroyOnLoad(SystemPrefabs[0]);
}

public void SetGameState(GameState state){
	this.gameState = state;
	OnStateChange();
}

public void loadScene(string namelevel)
{
    StartCoroutine(LoadAsynchronously(namelevel));
}
IEnumerator LoadAsynchronously(string namelevel)
{

    AsyncOperation async = SceneManager.LoadSceneAsync(namelevel);
    async.completed += OnLoadOperationComplete;
    _loadOperations.Add(async);
    SystemPrefabs[0].SetActive(true);
        while (!async.isDone)
        {
            float progress = Mathf.Clamp01(async.progress / 0.9f);
            slider.value = progress;
            progressText.text = (progress * 100).ToString("F0") + "%";

            yield return null;

        }
    
}
private void OnLoadOperationComplete(AsyncOperation ao)
{
    Debug.Log("Load Compete.");
    if (_loadOperations.Contains(ao) == true)
    {
        _loadOperations.Remove(ao);
    }
}
private void UnLoadOperationComplete(AsyncOperation ao)
{
    Debug.Log("Load Destroy.");
}

public void UnloadLevel(string levelName)
{
    AsyncOperation ao = SceneManager.UnloadSceneAsync(levelName);
    SystemPrefabs[0].SetActive(false);
    ao.completed += UnLoadOperationComplete;
}
public void OnApplicationQuit(){
	SimpleGameManager.instance = null;
}

void InstantiateSystemPrefabs()
{
    GameObject prefabInstance;
    for (int i = 0; i < SystemPrefabs.Length; ++i)
    {
        prefabInstance = Instantiate(SystemPrefabs*);*

_instanciedSystemPrefabs.Add(prefabInstance);
}
}
}

Hello @perriguigui

I am also fairly new to Unity, and just came across your question, when I was trying to find an answer to my own GameManager questions.
In my humble opinion, I see 5 things that may cause a problem.


1)

missing a using directive
Add:

 using UnityEngine.Events;

2)

I’m not sure your Singleton is correct.
Your Code:

public class SimpleGameManager: Singleton { public SimpleGameManager() {} private static SimpleGameManager instance = null;

Possible Code Solution:

 public class SimpleGameManager:  Singleton<SimpleGameManager>

3)

When Loading a Scene, add it Additively.
To do this, add the argument: LoadSceneMode.Additive to your AsyncOperation.
Your Code:

AsyncOperation async = SceneManager.LoadSceneAsync(namelevel);

Possible Code Solution:

 AsyncOperation async = SceneManager.LoadSceneAsync(namelevel, LoadSceneMode.Additive);

The purpose of Loading Additively in this case is to keep the GameManager script loaded.
I’m assuming that you do have a Boot Scene, with the GameManager Script in it.


  • Your code: DontDestroyOnLoad(gameObject); is an additional safekeeping Unity Method, used to save the GameManager from being unloaded or destroyed, as I understand it.

  • I know there is controversy about using DontDestroyOnLoad(gameObject); but for lack of an understandable argument against it, I am using it as well.

  • Your Boot Scene would be in the Hierarchy. When you press Play in the Unity Editor, your code should add the new levelName, (but, see problem 5).

  • By adding the argument: LoadSceneMode.Additive, the previously loaded Boot Scene with the GameManager Script will remain loaded, and your new Scene levelName will also load.


4)

You have 3 different names for your AsyncOperation Variable:

  • operation

  • async

  • ao
    Possible Code Solution: use only one variable - such as ao.

  • Your 1st variable, operation is not
    used.

Your Code:

AsyncOperation operation;

This declares the Global Variable operation, but it is not used.
This may not cause an error, but I think it is good practice not to have unused variables in your code.
I have read that ao can be created as a Local Variable, instead of creating a Global Variable, which you have done in three of your methods.

  • Your 2nd variable async is used
    here, in your IEnumerator method
    when you Load the Scene.

Your Code:

AsyncOperation async = SceneManager.LoadSceneAsync(namelevel);

(Also, your Coroutine is used to calculate a Load Progress Bar? Do you also need a Coroutine for the UnLoad?)

  • Your 3rd variable ao is used in the
    three methods:

OnLoadOperationComplete, UnLoadOperationComplete, UnloadLevel
As I said, I am also new, but I believe that using two variable names for the same thing ( async and ao ) may cause a problem.


5)

No Level Name is provided in the Code


I am writing my GameManager from the Unity Tutorial, Swords and Shovels, written in 2017, and last updated: November 05, 2019
Here is the link to the GameManager part of the Tutorial.

https://learn.unity.com/tutorial/writting-the-gamemanager?uv=2019.3&projectId=5d0cd2a3edbc2a00212b99f1

You may also be using this Tutorial, since many of the variables are the same.


If anyone knows of a more current GameManager Tutorial, I would love to know about it!!!


Your question was posted on Mar 29, 2019, and today is Nov 1, 2020, so you may already have solved your problems.
If not, I hope what I have provide is correct, and helpful.
I welcome any corrections.
Regards,
NewbieOneKanobie