- Home /
Question by
Tubestorm · May 20, 2020 at 11:11 AM ·
coroutineloadingscene loadloading screencoroutine errors
Loading Screens are confusing
Hey!
I used a youtube tutorial to get the code I currently have. Before adding in the coroutine, I loaded the game scene directly into the playGame() class, and triggered it with a button, this worked decently, i need a loading scene as my mobile game has a-lot loading before the player spawns.
when I used the tutorial and added the coroutine everything went to shit, unity keeps freezing, forcing me to restart the program. I'm not sure what the problem is as it works well for the person in youtube.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MainMenu : MonoBehaviour
{
public Animator transition;
public float transitionTime = 1f;
public void PlayGame()
{
//LoadNextLevel();
StartCoroutine(LoadAsynchronously(1));
}
public void PlayTutorial()
{
StartCoroutine(LoadAsynchronously(2));
}
public void QuitGame()
{
Debug.Log("GAME QUIT");
Application.Quit();
}
public void LoadNextLevel()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); //loads the next scene on build index
}
IEnumerator LoadAsynchronously(int sceneIndex)
{
Debug.Log("GAME QUIT");
AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex);
while (!operation.isDone)
{
Debug.Log(operation.progress);
yield return null;
}
}
IEnumerator LoadLevel(int levelIndex)
{
//play animation
transition.SetTrigger("Start");
//wait
yield return new WaitForSeconds(transitionTime);
//load scene
SceneManager.LoadScene(levelIndex);
}
}
If i'm doing anything wrongly, help me out.
Comment