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. Dismiss Notice

Two values of same variable?

Discussion in 'Scripting' started by Potices, Aug 1, 2014.

  1. Potices

    Potices

    Joined:
    Dec 12, 2013
    Posts:
    7
    I'm building a game, but I suddenly got a big suprise. Something was changed, even though I did nothing at all.

    Code (CSharp):
    1. public class GameManager : MonoBehaviour {
    2.  
    3.     private bool showWinScreen;
    4.  
    5.     void Start() {
    6.         showWinScreen = false;
    7.     }
    8.  
    9. public void completeLevel()
    10.     {
    11.         print (showWinScreen); // For testing
    12.         showWinScreen = true;
    13.         print (showWinScreen); // For testing
    14.     }
    15.  
    16.     public void OnGUI() {
    17.         print(showWinScreen);
    18.     }
    completeLevel() is called by my player, when it collides with a door.

    The thing is, my OnGUI() always detect showWinScreen as false. When I call completeLevel() second time, both of its prints will be true, but OnGUI() keep printing false.

    showWinScreen isn't set at any other places. What is weird is the fact that the completeLevel() prints true both times, when OnGUI has just printed false, and will do it the frame after again. I can't see how a method have a different variable value, than another methods.

    I have tried to change the variable type to int, and the values to 1 (false) and 2 (true), but same story.

    I havn't had problems with it until today, and I don't understand anything.

    Can anybody explain what I can do to make this work? Am I wrong or is it Unity?
     
  2. hammil

    hammil

    Joined:
    Jun 5, 2013
    Posts:
    56
    Hmm.. That's certainly puzzling. One thing that I've learned for sure is that if something impossible appears to happen, it's still probably impossible! Double-check your code for other places where 'showWinScreen' can be set; most IDEs will have a tool that allows you to do this.

    However, there is one really annoying change that Unity has made recently - all fields are now serialized, regardless of whether they are public or not. It's a long shot, but you can try adding [NonSerialized] above your field, just to make sure there's no funny business there.
     
  3. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Can you show me your whole script.
    The issue should be in this script, since showWinScreen is a private member.
    try making it static, and see if it makes a difference.
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Maybe you have multiple scripts that each have their own private showWinScreen among them? Otherwise I'd like to see the whole script.
     
    Magiichan likes this.
  5. Potices

    Potices

    Joined:
    Dec 12, 2013
    Posts:
    7
    Tried to put [System.NonSerialized] before the variable, but it didn't change anything.
    I can't find any places where showWinScreen is set, where it should cause problems. But you guys can of course see the whole script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameManager : MonoBehaviour {
    5.     private int currentLevel;
    6.     private int unlockedLevel;
    7.     private int realLevel;
    8.  
    9.     [System.NonSerialized]
    10.     private bool showWinScreen;
    11.     private bool showLevelSelect = false;
    12.     private bool showPause = false;
    13.     private float winScreenHeight = Screen.height * 0.75f;
    14.     private float winScreenWidth = Screen.width * 0.75f;
    15.  
    16.     public GUISkin skin;
    17.     public GUISkin myskin;
    18.     public GUIStyle winScreenStyle;
    19.     public GUIStyle pauseStyle;
    20.  
    21.     void Start() {
    22.         //PlayerPrefs.SetInt ("Unlocked level", 1); // Use this for testing!
    23.         if (PlayerPrefs.GetInt ("Unlocked level") > 0) {
    24.             unlockedLevel = PlayerPrefs.GetInt ("Unlocked level");
    25.         } else {
    26.             unlockedLevel = 1;
    27.         }
    28.  
    29.         if (Application.loadedLevelName == "main_menu") {
    30.             showLevelSelect = true;
    31.         }
    32.         currentLevel = Application.loadedLevel;
    33.         realLevel = currentLevel;
    34.  
    35.         showWinScreen = false;
    36.     }
    37.  
    38.     public void completeLevel()
    39.     {
    40.         currentLevel++;
    41.         if (currentLevel > unlockedLevel) {
    42.             SaveGame();
    43.         }
    44.         realLevel = currentLevel - 1;
    45.         print (showWinScreen);
    46.         showWinScreen = true;
    47.     }
    48.  
    49.     private void LoadNextLevel() {
    50.         if (currentLevel > unlockedLevel) {
    51.             SaveGame();
    52.         }
    53.         Application.LoadLevel(currentLevel);
    54.     }
    55.  
    56.     private void SaveGame() {
    57.         PlayerPrefs.SetInt ("Unlocked level", currentLevel);
    58.     }
    59.  
    60.     public void OnGUI() {
    61.         GUI.skin = myskin;
    62.         print (showWinScreen);
    63.         if (showWinScreen) {
    64.             Rect winScreenRect = new Rect(Screen.width/2 - (winScreenWidth/2), Screen.height/2 - (winScreenHeight/2), winScreenWidth, winScreenHeight);
    65.             GUI.Box(winScreenRect, "");
    66.  
    67.             if (GUI.Button(new Rect(winScreenRect.x + winScreenRect.width - Screen.width/6.8f - 20, winScreenRect.y + winScreenRect.height - Screen.height/11f - 20, Screen.width/6.8f, Screen.height / 11f), "Next Level", winScreenStyle)) {
    68.                 showWinScreen = false;
    69.                 LoadNextLevel();
    70.             }
    71.             if (GUI.Button(new Rect(winScreenRect.x + 20, winScreenRect.y + winScreenRect.height - Screen.height/11f - 20, Screen.width/6.8f, Screen.height / 11f), "Select Level", winScreenStyle)) {
    72.                 showWinScreen = false;
    73.                 Application.LoadLevel("main_menu");
    74.             }
    75.  
    76.             GUI.Label(new Rect(Screen.width/2 - (winScreenWidth/2), Screen.height/2 - (winScreenHeight/2), winScreenWidth, Screen.height*0.20f), "Congratulations! \n You just completed level " + realLevel.ToString());
    77.         }
    78.  
    79.         if (showLevelSelect) {
    80.             float boxSize = Screen.width * 0.7f / 7f;
    81.             float offset = Screen.width * 0.15f;
    82.             float offsetOp = Screen.height * 0.03f;
    83.             float mellemrum = offset /25;
    84.  
    85.             int maxAllowedLevel = 6;
    86.             int nomRows = 2;
    87.             int nomCols = 3;
    88.            
    89.             for(var n=1; n < maxAllowedLevel;){
    90.                 for(var i=0; i<nomRows; i++){
    91.                     for(var j=0;j<nomCols; j++){
    92.                         GUI.enabled = n <= maxAllowedLevel;
    93.                         if (unlockedLevel >= n) {
    94.                             GUI.backgroundColor = new Color(0.0f, 0.76f, 0.415f, 1.0f);
    95.                             if(GUI.Button(new Rect(offset + j*boxSize + j*mellemrum,offsetOp + i*boxSize + i*mellemrum,boxSize,boxSize), n.ToString())){
    96.                                 showLevelSelect = false;
    97.                                 Application.LoadLevel(n);
    98.                             }
    99.                         } else {
    100.                             GUI.backgroundColor = new Color(0.906f, 0.298f, 0.235f, 1.0f);
    101.                             GUI.Button(new Rect(offset + j*boxSize + j*mellemrum,offsetOp + i*boxSize + i*mellemrum,boxSize,boxSize), n.ToString());
    102.                         }
    103.                         n++;
    104.                     }
    105.                 }
    106.             }
    107.  
    108.  
    109.         }
    110.  
    111.         if (!showPause) {
    112.             GUI.backgroundColor = new Color(0f,0f,0f,0.7f);
    113.             if(GUI.Button(new Rect(0,0,50,50), "P", pauseStyle)) {
    114.                 showPause = true;
    115.                 Time.timeScale = 0;
    116.             }
    117.         } else {
    118.             Rect pauseRect = new Rect(Screen.width/2 - (winScreenWidth/2), Screen.height/2 - (winScreenHeight/2), winScreenWidth, winScreenHeight);
    119.             GUI.Box(pauseRect, "");
    120.  
    121.             if (GUI.Button(new Rect(pauseRect.x + pauseRect.width - Screen.width/6.8f - 20, pauseRect.y + pauseRect.height - Screen.height/11f - 20, Screen.width/6.8f, Screen.height / 11f), "Continue", winScreenStyle)) {
    122.                 showPause = false;
    123.                 Time.timeScale = 1.0f;
    124.             }
    125.             if (GUI.Button(new Rect(pauseRect.x + 20, pauseRect.y + pauseRect.height - Screen.height/11f - 20, Screen.width/6.8f, Screen.height / 11f), "Quit", winScreenStyle)) {
    126.                 showPause = false;
    127.                 Time.timeScale = 1.0f;
    128.                 Application.LoadLevel("main_menu");
    129.             }
    130.  
    131.             GUI.Label(new Rect(Screen.width/2 - (winScreenWidth/2), Screen.height/2 - (winScreenHeight/2), winScreenWidth, Screen.height*0.20f), "Game Paused!" );
    132.         }
    133.  
    134.  
    135.     }
    136. }
     
  6. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Can you show me the script that calls the CompletedLevel function?
    because there doesn't seem to be a problem with thisone ;o
     
  7. Potices

    Potices

    Joined:
    Dec 12, 2013
    Posts:
    7
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour {
    5.  
    6.     private Vector2 spawn;
    7.     private Quaternion spawnRotation;
    8.     public GameManager manager;
    9.  
    10.     void Start () {
    11.         spawn = transform.position;
    12.         spawnRotation = transform.rotation;
    13.         manager = manager.GetComponent<GameManager> ();
    14.         gameCompleted = false;
    15.     }
    16.  
    17.     void Update () {
    18.         transform.rotation = spawnRotation;
    19.     }
    20.  
    21.     void OnCollisionEnter2D (Collision2D other){
    22.         if (other.transform.tag == "Enemy") {
    23.             transform.position = spawn;
    24.         }
    25.     }
    26.  
    27.     void OnTriggerEnter2D (Collider2D other) {
    28.         if (other.transform.tag == "Checkpoint") {
    29.             spawn = other.transform.position;
    30.         }
    31.         if (other.transform.tag == "Goal") {
    32.                 manager.completeLevel();
    33.         }
    34.     }
    35. }
    Here you go.
     
  8. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    @Potices
    Let's first check whether the function get's called or not.
    Code (CSharp):
    1.         if (other.transform.tag == "Goal") {
    2.                 manager.completeLevel();
    3.                 Debug.Log("CompleteLevel has been called!");
    4.         }
     
  9. Potices

    Potices

    Joined:
    Dec 12, 2013
    Posts:
    7
    Tested it: it is called. Also tested inside completeLevel(). It's called too.

    I tried to change showWinScreen to true in Start(), and to false in completeLevel(). It results in the variable is true all the time at onGUI, but false ( second time) in completeLevel.

    Also tried to print showWinScreen at the Update function. It returns the same value as onGUI...

    I don't get this. Should I try reinstalling Unity, or something else?
     
  10. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Did you get any error messages pop up in the console? o.o
     
  11. Potices

    Potices

    Joined:
    Dec 12, 2013
    Posts:
    7
    No errors, only the logs.
     
  12. Potices

    Potices

    Joined:
    Dec 12, 2013
    Posts:
    7
    Yaah! Finally found a solution! Tried to change it to Static as you said in the First post, and it now works like it should!

    Thanks a lot for the help.

    Can't See how this was nessecary btw, but the most important thing is just that it is working now.
     
    Magiichan likes this.