Scene wont appear

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Fall : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == “Player”)
{
SceneManager.LoadScene(“GameOver”);
StartCoroutine(Wait());
}
}
public IEnumerator Wait()
{
yield return new WaitForSeconds(2);
SceneManager.LoadScene(“Start”);
}
}

It was supossed to load the GameOver then go back to the Start but it only loads the GameOver.
If i try with the GameOver as a comment its works fine but without it the Start doesnt appear.

Your script is attached to a game object in your current scene. It gets destroyed when you enter GameOver scene, that’s why the coroutine doesn’t execute fully and you don’t load the StartScene.

So how should i change the scene after the object being destroyed?

Either use a singleton or create a new script that switches to StartScene after 2sec and place it in your GameOver scene

I tried using DontDestroyOnLoad but i never used so it was kinda messy, i ended up using another script to change the scene. Thanks a lot.

1 Like