1

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");
    }
}
John Hamilton
  • 3,116
  • 1
  • 15
  • 34
LifGwaethrakindo
  • 1,421
  • 2
  • 14
  • 26
  • Try using Scene scene = SceneManager.GetActiveScene(); SceneManager.LoadScene(scene.name); instead of the name. – John Hamilton Dec 09 '16 at 21:53
  • 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? – LifGwaethrakindo Dec 09 '16 at 21:56
  • There could be many methods to achieve this. See: http://gamedev.stackexchange.com/questions/133872/how-to-store-variables-in-between-scenes-unity/133880#133880 – John Hamilton Dec 09 '16 at 21:58
  • 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. – Nick Dec 09 '16 at 22:12
  • 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... – LifGwaethrakindo Dec 09 '16 at 23:03
  • So, all variables that I declare on the start of the scene need to access the persistent object you propose? – LifGwaethrakindo Dec 09 '16 at 23:06
  • Im flagging this question to be closed. I copied your code into an empty project, ensured the names matched up, and sure enough - it worked perfectly. To be on topic, programming related questions must provide "a minimal *verifiable* example of the problem". Given your script works, you have not provided a verifiable example. – Gnemlock Dec 09 '16 at 23:11
  • My guess is you havn't actually added the scenes to build; or alternativley, your getting the names wrong. Perhaps you could provide a screenshot of your build settings to clarify any confusion? – Gnemlock Dec 09 '16 at 23:12
  • Here is a screen recording of your script working correctly. Origin level contains a simple idling alien, the second level simply contains a giant red cube. – Gnemlock Dec 09 '16 at 23:16
  • the problem goes once I replay the scene from other scene – LifGwaethrakindo Dec 09 '16 at 23:25
  • @Gnemlock , he mean to delete everything in the scene , but leave one object with script attached , and script attached to the object must be only one . upvoting this post . – user6668201 Dec 10 '16 at 04:12
  • @LinkWindcrafter if you're calling a scene from one scene and trying to call another scene from that scene, you won't be able to. Unity unloads all scenes in the editor. Make a build and it should work. – John Hamilton Dec 10 '16 at 05:32

1 Answers1

4

...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.

l'-
  • 627
  • 5
  • 10