Search Unity

[SOLVED] C# private global variable value changing by itself

Discussion in 'Scripting' started by brainfire9, Mar 25, 2016.

  1. brainfire9

    brainfire9

    Joined:
    Feb 3, 2015
    Posts:
    11
    Hi All,

    I've run into a really strange problem in Unity Personal 5.3.2f1, some of the global variables I am using in my scripts seem to be resetting (so far an integer and a boolean, both private).

    This script is on an empty GameObject and was working fine for a while, I prefab'd some of my objects (including this GameManager) and now I'm seeing this problem.

    In this example I was originally getting a return of 4 on line 13 (4 scenes in my game), started noticing that my "Next Level" button (which calls LoadNextLevel() ) was reloading the same level over and over after making this into a prefab. I decided to define numberOfScenes as 4 on line 14, still getting the same result. Added some Debug.Log statements and a watch variable and saw that my numberOfScenes value seems to initialize just fine but goes to zero right as I get into LoadNextLevel() at line 38. If I set this to Public I can actually see that numberOfScenes stays 4 in the inspector, yet shows 0 in my Debug.Log statement and in the watch window so I'm thinking I've got something going on here.

    My workaround for now is to set the value at 4 again in line 43, but I'm wondering if I've made some really silly mistake somewhere or if I should really reload a backup of my project from before this mess started and start over from there. I can also make this work correctly by changing Line 7 to "public int numberOfScenes = 4", but I'd really rather detect the number of levels.

    I have deleted the GameManager from this scene, created a new empty GameObject, added this script to it, and still get the same result.

    Does anything jump out as to why this value would be changing from 4 back to 0?

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class GameManager : MonoBehaviour
    6. {
    7.     private int numberOfScenes;
    8.  
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.         // TODO fix the scene count
    13.         //numberOfScenes = SceneManager.sceneCountInBuildSettings;
    14.         numberOfScenes = 4;
    15.         Debug.Log("Number of scenes: " + numberOfScenes);
    16.     }
    17.    
    18.     public void ReloadLevel()
    19.     {
    20.         // Disable "Next Level" button and reload the current scene
    21.  
    22.         FindObjectOfType<NextLevelButton>().Disable();
    23.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    24.     }
    25.  
    26.     public void QuitGame()
    27.     {
    28.         Debug.Log ("Quitting game!");
    29. #if UNITY_EDITOR
    30.         if (UnityEditor.EditorApplication.isPlaying)
    31.             UnityEditor.EditorApplication.isPlaying = false;
    32.         else
    33. #endif
    34.             Application.Quit();
    35.  
    36.     }
    37.  
    38.     public void LoadNextLevel()
    39.     {
    40.         int thisLevel, nextLevel;
    41.  
    42.         // TODO fix numberOfScenes
    43.         //numberOfScenes = 4;
    44.         Debug.Log("Number of scenes: " + numberOfScenes);
    45.         thisLevel = SceneManager.GetActiveScene().buildIndex;
    46.  
    47.         if (thisLevel >= numberOfScenes - 1)
    48.             nextLevel = 0;
    49.         else
    50.             nextLevel = thisLevel + 1;
    51.  
    52.         Debug.Log ("Loading scene #" + nextLevel);
    53.         SceneManager.LoadScene(nextLevel);
    54.     }
    55. }
     
  2. mathias234

    mathias234

    Joined:
    Sep 9, 2012
    Posts:
    239
    I cannot see what would change it that early on, as the only assign to 0 is after the debug. might be a bug in the editor. try to put the script in a new empty project
     
  3. Austin-Gregory

    Austin-Gregory

    Joined:
    Sep 19, 2013
    Posts:
    78
    I'm not getting what you're getting. Something is happening somewhere else.
     
  4. brainfire9

    brainfire9

    Joined:
    Feb 3, 2015
    Posts:
    11
    Thanks guys, I think something has happened to my project -- better to catch it now. I just created a new project with only the GameManager object and script and it works as expected. I'll create a new project and start bringing my assets over, should have thought of trying that in the first place.

    Thanks again!