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. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

"DontDestroyOnLoad" Object: Troubles with modifying associated variables

Discussion in 'Scripting' started by TurduckenMan, Jul 2, 2017.

  1. TurduckenMan

    TurduckenMan

    Joined:
    Apr 20, 2014
    Posts:
    29
    Hello, I've created an object+script named "DayManager". This object also has a script named "RemainOnLoad", which contains the following code:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class RemainOnLoad : MonoBehaviour {
    7.  
    8.     public static RemainOnLoad instance;
    9.  
    10.     void Start() {
    11.  
    12.         if (instance == null) {
    13.             instance = this;
    14.             DontDestroyOnLoad(gameObject);
    15.         } else if (instance != this)
    16.             Destroy(gameObject);
    17.        
    18.     }
    19. }
    20.  
    21.  
    The idea is that the object persists throughout scenes, and when returning to the original scene, there are no duplicates of the object. This works as intended.

    Now, within my "DayManager" script, I've turned it into an accessible script by using "public static ScriptName instance". This also works as intended.

    My issue comes up in the following situation: At the end of an in-game day, the game loads a new scene. From that second scene, there is a button that if clicked will return the player back to the first scene. (This second scene has other options as well, it does have a purpose.). Right before the first scene is reloaded, an int named "currentDay" is increased by one (currentDay++). This works, the first time. Once the first scene has been reloaded, and the new "Day" has ended, going through the end of day stuff, switching to a different scene and then back, for some reason does not cause the currentDay variable to increase anymore.

    Could this be because the variable is for some reason resetting everytime the first scene is reloaded? (Even though there are no duplicates of the DayManager object)

    Any help would be very much appreciated. I've included the relevant scripts below.

    The button that returns the player to the first scene contains the following code:

    Code (csharp):
    1.  
    2. public void EndDayButton () {
    3.  
    4.         DayManager.instance.BeginNewDay();
    5.  
    6.     }
    7.  
    My entire "DayManager" script (Obviously not all relevant, but I wanted to inclue it to be sure I didn't omit something important):

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class DayManager : MonoBehaviour {
    8.  
    9.     public static DayManager instance;
    10.  
    11.     [Header("Day Variables")]
    12.     public int totalDays;
    13.     //[HideInInspector]
    14.     public int currentDay = 1;
    15.  
    16.     void Awake () {
    17.  
    18.         instance = this;
    19.  
    20.     }
    21.  
    22.     void Start () {
    23.        
    24.         TickManager.instance.SetTickPause(false);
    25.  
    26.     }
    27.  
    28.     public void EndDay () {
    29.  
    30.         TickManager.instance.SetTickPause(true);
    31.  
    32.         if (currentDay < totalDays) {
    33.            
    34.             SceneManager.LoadScene("Night View");
    35.             //BeginNewDay();
    36.  
    37.         } else {
    38.  
    39.             //Final day stuff
    40.             Debug.Log("Game end.");
    41.  
    42.         }
    43.  
    44.     }
    45.  
    46.     public void BeginNewDay() {
    47.        
    48.         currentDay++;
    49.         SceneManager.LoadScene("Office");
    50.  
    51.     }
    52.  
    53. }
    54.  
    55.  
     
  2. TurduckenMan

    TurduckenMan

    Joined:
    Apr 20, 2014
    Posts:
    29
    Issues been solved. Needed to say if (instance != null) in awake before setting instance = this;