0
\$\begingroup\$

I am very new to Unity, and coding in general. Anyways, I'm trying to load another scene on click, but when I try to do so in the editor, Unity freezes. Also, it, by chance it seems, worked twice when I newly added the script. Not always when I newly add the script, but twice.

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class GameMenu : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

        if (Input.GetMouseButtonDown(0))
        {
            SceneManager.LoadSceneAsync(sceneName: "Wisp1");
        }

    }
}

Can somebody help?

\$\endgroup\$
1
  • \$\begingroup\$ There is not really enough information here to know exactly what the problem is, but one of the answer's below may point you in the right direction. \$\endgroup\$
    – Evorlor
    Sep 6, 2018 at 0:49

2 Answers 2

1
\$\begingroup\$

all you need is the string name of the scene so if your scene was named Wisp1

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        SceneManager.LoadSceneAsync("Wisp1");
    }
}

using a coroutine may be much smoother and is how the docs show loading async.

if (Input.GetMouseButtonDown(0))
{
    StartCoroutine(LoadYourAsyncScene());
}

IEnumerator LoadYourAsyncScene()
{
     AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("Wisp1");

    // Wait until the asynchronous scene fully loads
    while (!asyncLoad.isDone)
    {
        yield return null;
    }

}

double check build settings to make sure the scene is present. enter image description here

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You can improve on this further in two ways: 1. yield return asyncLoad; can replace your while loop. 2. Check whether the coroutine is still running before trying to start a new one. \$\endgroup\$
    – DMGregory
    Jul 13, 2018 at 2:40
0
\$\begingroup\$

'LoadSceneAsync' takes a third param which is the mode. It's 'Single' by default which means it will unload current scene. I'm pretty sure this is the reason for the hang. Try using additive mode

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .