1
\$\begingroup\$

I have a problem when I try to reset a scene. It doesn't seem to run, I have a script (of all scripts that have Awake and Start methods on that scene) that on the Awake method starts a coroutine, but when reseting the scene it doesn't even run it. I've read that it may be a problem of using the DontDestroyOnLoad method, but that doesn't seem to solve the problem since that script on the start takes a lot of references of objects that are only on the scene that I want to restart. I don't use any static variables on any script.

I made an unitary test trying to restart a scene with just one object and one script attached, on its Start method it also starts a coroutine but even in that test it doesn't even run the coroutine.

Here's the script of the unitary test:

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

public class LoadingSceneController : MonoBehaviour
{
    void Awake()
    {
        DontDestroyOnLoad(this.gameObject);
    }
    //Scene scene;
    // Use this for initialization
    void Start()
    {
        //scene = SceneManager.GetActiveScene();
        DontDestroyOnLoad(this.gameObject);
        useStartCoroutine();
    }

    void useStartCoroutine()
    {
        StartCoroutine(loadScene());
    }

    IEnumerator loadScene()
    {
        yield return new WaitForSeconds(3);
        SceneManager.LoadScene("gameplayScene 1");
        //SceneManager.LoadSceneAsync("gameplayScene 1");
    }
}
\$\endgroup\$
  • \$\begingroup\$ Try using Scene scene = SceneManager.GetActiveScene(); SceneManager.LoadScene(scene.name); instead of the name. \$\endgroup\$ – John Hamilton Dec 9 '16 at 21:53
  • \$\begingroup\$ that would be a solution for the case on which I would try to reset the scene from the active scene. But what happens if I want so replay the scene from another scene. Should I store the scene in a variable then? \$\endgroup\$ – LinkWindcrafter Dec 9 '16 at 21:56
  • \$\begingroup\$ There could be many methods to achieve this. See: gamedev.stackexchange.com/questions/133872/… \$\endgroup\$ – John Hamilton Dec 9 '16 at 21:58
  • \$\begingroup\$ Are you sure that you have added scene in Build section? and i prefer you using indexes of scenes instead of names, just to be sure. \$\endgroup\$ – Nick Dec 9 '16 at 22:12
  • \$\begingroup\$ Yeah, the scene is added on build settings, when I load the scene for the first time it runs normal. The second time I try to load it its when it doesn't run... \$\endgroup\$ – LinkWindcrafter Dec 9 '16 at 23:03
4
\$\begingroup\$

...the Awake method starts a coroutine, but when reseting the scene it doesn't even run it.

The Awake method is only called when a script is first loaded. Because your script has DontDestroyOnLoad (which you don't need to put in Start because you already have it in Awake), it will be carried over to any new scene that you load, and it will not be loaded again, so Awake will not be called again.

What you want is a method that subscribes to the SceneManager.sceneLoaded event:

private void OnEnable()
{
    SceneManager.sceneLoaded += OnLevelLoaded;
}

private void OnDisable()
{
    SceneManager.sceneLoaded -= OnLevelLoaded;
}

private void OnLevelLoaded(Scene scene, LoadSceneMode loadSceneMode)
{
    Debug.Log(string.Format("The level '{0}' was loaded.", scene.name));
}

This code will be called between the Awake and the Start methods when loading a scene.

A bit unrelated, but it's important to note that, because your original code has DontDestroyOnLoad, if you switch from a scene that has your LoadingSceneController object within it to a different scene, and then switch back to that scene, it will create a second instance of your LoadingSceneController object. You can fix this by spawning your LoadingSceneController object through code only once (if it doesn't already exist), or adding some code at the beginning of your script's Awake that will destroy its gameObject if another object with that script already exists.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.