Search Unity

Boolean and Integer value won't change from one script to other.

Discussion in 'Scripting' started by Batka, Nov 30, 2016.

  1. Batka

    Batka

    Joined:
    May 23, 2016
    Posts:
    22
    Hey guys,

    Okay so I have this bug that I really don't get the logic off. It's either something ultra stupid, or something stack related I don't yet understand...

    Code (CSharp):
    1. public void LevelLoad(Level lev)
    2.         {
    3. #if UNITY_ANDROID && !UNITY_EDITOR && !UNITY_IOS
    4.             _audioManager.ReleasePool();
    5. #endif
    6.             currentlyDisappearedFigures = 0;
    7.             if (!lev.Equals(currentLevel))
    8.             {
    9.                 currentLevel = lev;
    10.  
    11.                 foreach (Figure fig in lev.Figures)
    12.                 {
    13.                     if (fig._figureType != null)
    14.                     {
    15.                         if (fig._gameObject.CompareTag("Hard"))
    16.                             fig._figureType.ResetHard();
    17.                         fig._figureType.RefreshFigure();
    18.                         lev.totalQTY++;
    19.                     }
    20.                     Debug.Log("HasNormal-" + lev.hasNormal + ",normalQTY-" + lev.normalQTY + ",hasTransparent-" +
    21.                     lev.hasTransparent + ",transparentlQTY-" + lev.transparentQTY + ",hasND-" +
    22.                     lev.hasND + ",hasDeath-" + lev.hasDeath);
    23.                 }
    24.                
    25.                 if (lev.Hint != null)
    26.                     lev.Hint.SetActive(false);
    27.  
    28.                 isCurrentLevelPassed = false;
    29.             }
    30. #if UNITY_ANDROID && !UNITY_EDITOR && !UNITY_IOS
    31.             if (currentLevel.totalQTY > 22)
    32.                 currentLevel.totalQTY = 22;
    33.             _audioManager.CreatePool(lev.totalQTY + 1);
    34.             Debug.Log("currentLeve.totalQTY-" + currentLevel.totalQTY);
    35.  
    36.             _audioManager.LoadAudio(currentLevel.hasNormal, currentLevel.normalQTY,
    37.                 currentLevel.hasTransparent, currentLevel.transparentQTY,
    38.                 currentLevel.hasND, currentLevel.hasDeath);
    39.         Debug.Log("hasNormal-" + currentLevel.hasNormal + ",normalQTY-" + currentLevel.normalQTY + ",hasTransparent-" +
    40.                 currentLevel.hasTransparent + ",transparentlQTY-" + currentLevel.transparentQTY + ",hasND-" +
    41.                 currentLevel.hasND + ",hasDeath-" + currentLevel.hasDeath);
    42.  
    43. #endif
    44.     }
    See the:

    Code (csharp):
    1. Debug.Log("HasNormal-" + currentLevel.hasNormal + ",normalQTY-" + currentLevel.normalQTY
    2. + ",hasTransparent-" + currentLevel.hasTransparent
    3. + ",transparentlQTY-" + currentLevel.transparentQTY
    4. + ",hasND-" + currentLevel.hasND
    5. + ",hasDeath-" + currentLevel.hasDeath);


    All of those print either FALSE, or ZERO, and I don't know why.

    ----------

    The thing is, I am instantiating them in:

    Code (csharp):
    1. fig._figureType.RefreshFigure();
    ----------
    Here is the inside of FigureType.RefreshFigure(). It's irrelevant, but the Debug.Log inside it shows different values from the one I mentioned above:

    Code (csharp):
    1. public void RefreshFigure()
    2. {
    3.       gameObject.SetActive(true);
    4.       Level currentLevel = GameManager.Instance.GetCurrentLevel();
    5.       _collider.enabled = true;
    6.       if(_type != FigType.Death && _type != FigType.Transparent)
    7.            _collider.isTrigger = false;
    8.       ResetTransform();
    9.            
    10.    
    11.     if (_type == FigType.Normal)
    12.         {
    13.            currentLevel.hasNormal = true;
    14.            currentLevel.normalQTY++;
    15.            Debug.Log("Current Level hasNormal inside RefreshFigure(): " + currentLevel.hasNormal);
    16.            Debug.Log("Current Level Normal QTY inside RefreshFigure(): " + currentLevel.normalQTY);
    17.         }
    18.      if (_type == FigType.Transparent)
    19.       {
    20.          currentLevel.hasTransparent = true;
    21.          currentLevel.transparentQTY++;
    22.       }
    23.    if (_type == FigType.Hard)
    24.       {
    25.          currentLevel.hasNormal = true;
    26.          for(int i = 0; i < hardLives; i++)
    27.                currentLevel.normalQTY++;
    28.         }
    29.         if (_type == FigType.NotDisappear)
    30.             currentLevel.hasND = true;
    31.         if (_type == FigType.Death)
    32.             currentLevel.hasDeath = true;
    33.    
    34.         StartCoroutine(Tools.MakeTransparent(_spriteRenderer, 1, 0.5F));
    35.         Debug.Log("Current Level hasNormal on end of RefreshFigure(): " + currentLevel.hasNormal);
    36.         Debug.Log("Current Level Normal QTY on end of RefreshFigure(): " + currentLevel.normalQTY);

    Here, take a look at my log entry:

    http://prnt.sc/ddrbqr

    ----------


    **Here are some notes I'd like to say about this:**

    *Level* is a **struct**, not a **class.**

    I've removed annotations like #if UNITY_ANDROID, just to check if those are the reason.

    I've also tested on Android device, same bug.

    Here are all references to the boolean .hasNormal, none of which sets it to false:

    http://prntscr.com/ddrf5e


    Please guys, make me feel stupid. :)

    Thanks a lot for your time!


    TL;DR: a Boolean and an Integer refuse to be changed :)
     
  2. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Wish granted! Here is your problem:

    Code (csharp):
    1. public void RefreshFigure()
    2. {
    3.       gameObject.SetActive(true);
    4. -------->    Level currentLevel = GameManager.Instance.GetCurrentLevel();   <--------
    5.  
    You're creating a new variable named "currentLevel" that is being used instead of the "currentLevel" member of your class. Get rid of the "Level" so it's just:

    Code (csharp):
    1. currentLevel = GameManager.Instance.GetCurrentLevel();
     
  3. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Also, I'm not sure if you're aware since it's hard to see what you're doing in this point, but when you do:
    Code (csharp):
    1. currentLevel = lev;
    and then make changes to lev, those changes will not show up in currentLevel. With structs, when you say "currentLevel = lev", it is making a new copy of the struct, not just pointing to the old one the way it would with a class.
     
  4. Batka

    Batka

    Joined:
    May 23, 2016
    Posts:
    22
    RefreshFigure is in one class, and LoadLevel() is in class named Obstacles.

    What the function
    Code (csharp):
    1. GameManager.Instance.GetCurrentLevel();
    does is
    Code (csharp):
    1. return obstacles.currentLevel;
    So I have to mention it's a Level.


    Yea I thought the same, if I am using wrong references, but I did this to make sure:
    http://prntscr.com/dducyt

    Both show zero and false.

    Anyway, I did all of those in a separate function, inside obstacles, and it's okay now:

    Code (CSharp):
    1. void FindLevelFigureTypes()
    2. {
    3.     Debug.Log("Time on the begining: " + Time.time);
    4.     currentLevel.transparentQTY = 0; currentLevel.normalQTY = 0; currentLevel.totalQTY = 0;
    5.     currentLevel.hasDeath = false; currentLevel.hasND = false; currentLevel.hasNormal = false; currentLevel.hasTransparent = false;
    6.     foreach (Figure fig in currentLevel.Figures)
    7.     {
    8.         currentLevel.totalQTY++;
    9.         if (fig._figureType != null)
    10.         {
    11.             string tag = fig._gameObject.tag;
    12.             if(tag == Constants.TagNormal)
    13.             {
    14.                 currentLevel.hasNormal = true;
    15.                 currentLevel.normalQTY++;
    16.             }
    17.             else if(tag == Constants.TagHard)
    18.             {
    19.                 currentLevel.hasNormal = true;
    20.                 currentLevel.normalQTY += fig._figureType.hardLives;
    21.             }
    22.             else if (tag == Constants.TagDeath)
    23.             {
    24.                 currentLevel.hasDeath = true;
    25.             }
    26.             else if (tag == Constants.TagNotDisappear)
    27.             {
    28.                 currentLevel.hasND = true;
    29.             }
    30.          }
    31.    }
    32.    Debug.Log("Time at the end: " + Time.time);
    33.    Debug.Log("Inside FIND... hasNormal-" + currentLevel.hasNormal + ",normalQTY-" + currentLevel.normalQTY + ",hasTransparent-" +
    34.         currentLevel.hasTransparent + ",transparentlQTY-" + currentLevel.transparentQTY + ",hasND-" +
    35.         currentLevel.hasND + ",hasDeath-" + currentLevel.hasDeath);
    36. #if UNITY_ANDROID && !UNITY_EDITOR && !UNITY_IOS
    37.     if (currentLevel.totalQTY > 22)
    38.         currentLevel.totalQTY = 22;
    39.     _audioManager.CreatePool(currentLevel.totalQTY + 1);
    40.     Debug.Log("currentLeve.totalQTY-" + currentLevel.totalQTY);
    41.     _audioManager.LoadAudio(currentLevel.hasNormal, currentLevel.normalQTY,
    42.         currentLevel.hasTransparent, currentLevel.transparentQTY,
    43.         currentLevel.hasND, currentLevel.hasDeath);
    44.     Debug.Log("hasNormal-" + currentLevel.hasNormal + ",normalQTY-" + currentLevel.normalQTY + ",hasTransparent-" +
    45.         currentLevel.hasTransparent + ",transparentlQTY-" + currentLevel.transparentQTY + ",hasND-" +
    46.         currentLevel.hasND + ",hasDeath-" + currentLevel.hasDeath);
    47.  
    48.  
    49. #endif
    50. }
    Thanks for your help!
    And I am still curious what's happening...