Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

[SOLVED] DontDestroyOnLoad function

Discussion in 'Scripting' started by tanoille2, Mar 19, 2020.

  1. tanoille2

    tanoille2

    Joined:
    Mar 12, 2020
    Posts:
    10
    Hello! I'm new to coding and i have a problem with DontDestroyOnLoad function
    So the game over UI pop then i reload to main menu but my time,lives and game over UI still intact.
    How do i make them disappear after i load to my main menu.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3.  
    4. public class GameMaster : MonoBehaviour
    5. {
    6.  
    7.     static GameMaster current;
    8.        
    9.     [SerializeField]
    10.     private int maxLives = 3;
    11.     private static int _remainingLives;
    12.     public static int RemainingLives
    13.     {
    14.         get { return _remainingLives; }
    15.     }
    16.  
    17.     public float deathSequenceDuration = 1.5f;
    18.  
    19.     float totalGameTime;                      
    20.     public float spawnDelay = 1;
    21.     [SerializeField]
    22.     private GameObject gameOverUI = null;
    23.  
    24.  
    25.     void Start()
    26.     {
    27.         _remainingLives = maxLives;
    28.     }
    29.  
    30.     void Awake()
    31.     {
    32.        
    33.         if (current != null && current != this)
    34.         {
    35.            
    36.             Destroy(gameObject);
    37.             return;
    38.         }
    39.        
    40.         current = this;
    41.        
    42.         DontDestroyOnLoad(gameObject);
    43.     }
    44.     void Update()
    45.     {
    46.        
    47.         if (_remainingLives <= 0)
    48.             return;
    49.  
    50.         totalGameTime += Time.deltaTime;
    51.         UI.UpdateTimeUI(totalGameTime);
    52.     }
    53.     public void EndGame()
    54.     {
    55.         Debug.Log("GAME OVER");
    56.         gameOverUI.SetActive(true);
    57.     }
    58.     public static void PlayerDied(Player player)
    59.     {
    60.         Destroy(player.gameObject);
    61.         _remainingLives -= 1;
    62.         if(_remainingLives <= 0)
    63.         {
    64.             current.EndGame();
    65.         }
    66.         else
    67.         {
    68.             current.Invoke("RestartScene", current.deathSequenceDuration);
    69.         }
    70.     }
    71.  
    72.     void RestartScene()
    73.     {
    74.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    75.     }
    76. }
    77.  
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi @akopositano,

    If you have made those UI elements (and maybe some other things) DontDestroyOnLoad, you might want to make some event you trigger/method call and that will then trigger self-destruct or hide in your items you want to vanish.
     
    tanoille2 likes this.
  3. tanoille2

    tanoille2

    Joined:
    Mar 12, 2020
    Posts:
    10
    Hello @Olmi,

    Dude that solves everything!


    (How do i mark your answer as correct?)
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Well you can start by clicking that Like button ;).

    And if you want to tag it somehow solved, sometimes I've seen people edit the title and add [Solved] or something like that. i.e. "[SOLVED] I need help!".

    But I'm not sure what an official way is, if there's such.
     
    tanoille2 likes this.