Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by perriguigui · Mar 29, 2019 at 12:41 AM · hudasyncloading screenloadlevelasync

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 :-)

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[i]);
         _instanciedSystemPrefabs.Add(prefabInstance);
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by NewbieOneKanobie · Nov 01, 2020 at 03:48 PM

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≺ojectId=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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

104 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Loading Bar in Different Scenes 1 Answer

Loading Bar for Awake Function 1 Answer

LoadLevelAsync movie not playing gui!! 1 Answer

Got a problem with Async! 0 Answers

Move an object from start to end position depending on the progress of loadlevel..async 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges