Question by
ao196419 · May 28, 2017 at 03:15 PM ·
scene-loadingscene-switchingstartscene loadscene change
Start function not called after reloading scene
Hi! So I have 2 scenes that are switched back and forth from time to time. However, when a scene is loaded the second time, its Start function is not called. Here is a part of the code:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class StartScene : MonoBehaviour {
IEnumerator loading(){
AsyncOperation loadLevel = SceneManager.LoadSceneAsync ("Maze");
while (!loadLevel.isDone) {
yield return null;
}
}
void Start () {
Debug.Log ("Hi this is Start()");
}
void Update () {
if (Input.GetKeyDown ("enter") || Input.GetKeyDown ("return")) {
StartCoroutine (loading ());
}
}
The line of code Debug.Log ("Hi this is Start()");
is not executed if the code have been loaded before.
Things that I tried:
Use
SceneManager.UnloadSceneAsync("StartScene")
; to delete all GameObject before switching to the next scene.Use this to delete all GameObjects before switching to the next scene.
foreach (GameObject o in Object.FindObjectsOfType<GameObject>()) { Destroy(o); }
Put
this.gameObject.GetComponent<StartScene> ().enabled = true;
in Awake().
But nothing works :( Any help will be greatly appreciated!
Comment