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

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…

public void LevelLoad(Level lev)
        {
#if UNITY_ANDROID && !UNITY_EDITOR && !UNITY_IOS
            _audioManager.ReleasePool();
#endif
            currentlyDisappearedFigures = 0;
            if (!lev.Equals(currentLevel))
            {
                currentLevel = lev;

                foreach (Figure fig in lev.Figures)
                {
                    if (fig._figureType != null)
                    {
                        if (fig._gameObject.CompareTag("Hard"))
                            fig._figureType.ResetHard();
                        fig._figureType.RefreshFigure();
                        lev.totalQTY++;
                    }
                    Debug.Log("HasNormal-" + lev.hasNormal + ",normalQTY-" + lev.normalQTY + ",hasTransparent-" +
                    lev.hasTransparent + ",transparentlQTY-" + lev.transparentQTY + ",hasND-" +
                    lev.hasND + ",hasDeath-" + lev.hasDeath);
                }
               
                if (lev.Hint != null)
                    lev.Hint.SetActive(false);
  
                isCurrentLevelPassed = false;
            }
#if UNITY_ANDROID && !UNITY_EDITOR && !UNITY_IOS
            if (currentLevel.totalQTY > 22)
                currentLevel.totalQTY = 22;
            _audioManager.CreatePool(lev.totalQTY + 1);
            Debug.Log("currentLeve.totalQTY-" + currentLevel.totalQTY);
  
            _audioManager.LoadAudio(currentLevel.hasNormal, currentLevel.normalQTY,
                currentLevel.hasTransparent, currentLevel.transparentQTY,
                currentLevel.hasND, currentLevel.hasDeath);
        Debug.Log("hasNormal-" + currentLevel.hasNormal + ",normalQTY-" + currentLevel.normalQTY + ",hasTransparent-" +
                currentLevel.hasTransparent + ",transparentlQTY-" + currentLevel.transparentQTY + ",hasND-" +
                currentLevel.hasND + ",hasDeath-" + currentLevel.hasDeath);
  
#endif
    }

See the:

Debug.Log("HasNormal-" + currentLevel.hasNormal + ",normalQTY-" + currentLevel.normalQTY 
+ ",hasTransparent-" + currentLevel.hasTransparent 
+ ",transparentlQTY-" + currentLevel.transparentQTY
+ ",hasND-" + currentLevel.hasND 
+ ",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:

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:

public void RefreshFigure()
{
      gameObject.SetActive(true);
      Level currentLevel = GameManager.Instance.GetCurrentLevel();
      _collider.enabled = true;
      if(_type != FigType.Death && _type != FigType.Transparent)
           _collider.isTrigger = false;
      ResetTransform();
           
   
    if (_type == FigType.Normal)
        {
           currentLevel.hasNormal = true;
           currentLevel.normalQTY++;
           Debug.Log("Current Level hasNormal inside RefreshFigure(): " + currentLevel.hasNormal);
           Debug.Log("Current Level Normal QTY inside RefreshFigure(): " + currentLevel.normalQTY);
        }
     if (_type == FigType.Transparent)
      {
         currentLevel.hasTransparent = true;
         currentLevel.transparentQTY++;
      }
   if (_type == FigType.Hard)
      {
         currentLevel.hasNormal = true;
         for(int i = 0; i < hardLives; i++)
               currentLevel.normalQTY++;
        }
        if (_type == FigType.NotDisappear)
            currentLevel.hasND = true;
        if (_type == FigType.Death)
            currentLevel.hasDeath = true;
   
        StartCoroutine(Tools.MakeTransparent(_spriteRenderer, 1, 0.5F));
        Debug.Log("Current Level hasNormal on end of RefreshFigure(): " + currentLevel.hasNormal);
        Debug.Log("Current Level Normal QTY on end of RefreshFigure(): " + currentLevel.normalQTY);

Here, take a look at my log entry:


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:

Please guys, make me feel stupid. :slight_smile:

Thanks a lot for your time!

TL;DR: a Boolean and an Integer refuse to be changed :slight_smile:

Wish granted! Here is your problem:

public void RefreshFigure()
{
      gameObject.SetActive(true);
-------->    Level currentLevel = GameManager.Instance.GetCurrentLevel();   <--------

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:

currentLevel = GameManager.Instance.GetCurrentLevel();

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:

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.

RefreshFigure is in one class, and LoadLevel() is in class named Obstacles.

What the function GameManager.Instance.GetCurrentLevel(); does is 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:

void FindLevelFigureTypes()
{
    Debug.Log("Time on the begining: " + Time.time);
    currentLevel.transparentQTY = 0; currentLevel.normalQTY = 0; currentLevel.totalQTY = 0;
    currentLevel.hasDeath = false; currentLevel.hasND = false; currentLevel.hasNormal = false; currentLevel.hasTransparent = false;
    foreach (Figure fig in currentLevel.Figures)
    {
        currentLevel.totalQTY++;
        if (fig._figureType != null)
        {
            string tag = fig._gameObject.tag;
            if(tag == Constants.TagNormal)
            {
                currentLevel.hasNormal = true;
                currentLevel.normalQTY++;
            }
            else if(tag == Constants.TagHard)
            {
                currentLevel.hasNormal = true;
                currentLevel.normalQTY += fig._figureType.hardLives;
            }
            else if (tag == Constants.TagDeath)
            {
                currentLevel.hasDeath = true;
            }
            else if (tag == Constants.TagNotDisappear)
            {
                currentLevel.hasND = true;
            }
         }
   }
   Debug.Log("Time at the end: " + Time.time);
   Debug.Log("Inside FIND... hasNormal-" + currentLevel.hasNormal + ",normalQTY-" + currentLevel.normalQTY + ",hasTransparent-" +
        currentLevel.hasTransparent + ",transparentlQTY-" + currentLevel.transparentQTY + ",hasND-" +
        currentLevel.hasND + ",hasDeath-" + currentLevel.hasDeath);
#if UNITY_ANDROID && !UNITY_EDITOR && !UNITY_IOS
    if (currentLevel.totalQTY > 22)
        currentLevel.totalQTY = 22;
    _audioManager.CreatePool(currentLevel.totalQTY + 1);
    Debug.Log("currentLeve.totalQTY-" + currentLevel.totalQTY);
    _audioManager.LoadAudio(currentLevel.hasNormal, currentLevel.normalQTY,
        currentLevel.hasTransparent, currentLevel.transparentQTY,
        currentLevel.hasND, currentLevel.hasDeath);
    Debug.Log("hasNormal-" + currentLevel.hasNormal + ",normalQTY-" + currentLevel.normalQTY + ",hasTransparent-" +
        currentLevel.hasTransparent + ",transparentlQTY-" + currentLevel.transparentQTY + ",hasND-" +
        currentLevel.hasND + ",hasDeath-" + currentLevel.hasDeath);


#endif
}

Thanks for your help!
And I am still curious what’s happening…