0
\$\begingroup\$

There is a logic to allow the scene activation later in the game after it was loaded. However, "sometimes", the scene would activate itself automatically. I tried debugging and found that the logic to activate the scene is not called at all. As a matter of fact, I use other classes that use threads to deal with networking (I'm suspecting they are the culprit). How do I prevent the scene from activating in this case?

The logic that set allowSceneActivation is in a coroutine which is called periodically every 0.2 seconds inside Update() as well. The threading parts are inside the class that gets the message, but they don't really do something inside this class. I put a breakpoint inside the part that set allowSceneActivation to true, but it wasn't hit when the new scene is activate.

What I notice is that the isDone member of the m_SceneLoadOperation is true while allowSceneActivation is still false. This should not have happened according to Unity document if I recalled correctly.

Update: Strangely, I just added a callback to Completed event of m_SceneLoadCoperation that does some logging. The symptom "seems to disappear" (Not sure because it occurred randomly). Now I'm even more confused.

Update: For whatever reasons, the symptom seems to disappear again after I update AddressableAsset package to version 1.7.5.

    private void Update()
    {
        if (!m_IsSceneLoaded)
        {
            m_SceneLoadOperation = SceneManager.LoadSceneAsync("Main Menu", LoadSceneMode.Single);
            m_SceneLoadOperation.allowSceneActivation = false;
            m_SceneLoadOperation.completed += (op) =>
            {
                Debug.Log("Scene Loaded.");
            };
            m_IsSceneLoaded = true;
        }
        if (Time.time - m_LastSendTime > 0.2f)
        {
            StartCoroutine(ConfirmConnection());
            m_LastSendTime = Time.time;
        }
        if (m_IsReadyToSwitchScene)
        {
            m_IsReadyToSwitchScene = false;
            Debug.Log("Switch to Waiting scene");
            if (!m_SceneLoadOperation.allowSceneActivation && !m_SceneLoadOperation.isDone)
            {
                m_SceneLoadOperation.allowSceneActivation = true;
            }
        }
    }

    private IEnumerator ConfirmConnection()
    {
        yield return StartCoroutine(SendHttpGET(url));
        string message = GetWebRequestMessage();
        if (message == "valid")
        {
            m_IsReadyToSwitchScene = true;
        }
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.