Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Scene swapping with delay

Discussion in 'Getting Started' started by CalebAO7, Apr 17, 2024.

  1. CalebAO7

    CalebAO7

    Joined:
    Feb 7, 2024
    Posts:
    4
    I'm trying to create a script that will, after a delay, swap the scene. Right now, the player character will move into the end point which will run a script that activates an animation with a level complete screen, and I'm trying to make it so that a few seconds after this run (so that the player can look at it for a second) the next scene will start. The script for the scene swap is in a completely different script from the one used to start the animation, so I have no clue how to call for it to be used in the Game Manager. Additionally, I'm also unsure of how to properly delay the script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class GameManager : MonoBehaviour
    7. {
    8.  
    9.     bool gameHasEnded = false;
    10.  
    11.     public float restartDelay = 1f;
    12.  
    13.     public GameObject completeLevelUI;
    14.  
    15.  
    16.     // Start is called before the first frame update
    17.     public void EndGame ()
    18.     {
    19.         if (gameHasEnded == false)
    20.         {
    21.             gameHasEnded = true;
    22.             Debug.Log("GAME OVER");
    23.             Invoke("Restart", restartDelay);
    24.         }
    25.  
    26.        
    27.     }
    28.  
    29.     void Restart()
    30.     {
    31.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    32.     }
    33.  
    34.  
    35.  
    36.     public void CompleteLevel()
    37.     {
    38.         completeLevelUI.SetActive(true);
    39.     }
    40.  
    41. }
    42. //This is the Game Mananger


    Code (CSharp):
    1. //This is the next scene script
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class NextLevel : MonoBehaviour
    9. {
    10.     // Start is called before the first frame update
    11.     public void LoadNextLevel()
    12.     {
    13.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    14.     }
    15. }
    16.