breakableCount not initialising after a lose (with unelegant fix?)(Fix in later lessons)

My code seems to be correct, I can’t find any errors in it, using the Print (breakableCount); i can see that the number of blocks when first playing a level is correct and the count is depreciating correctly, but if there’s a lose and return to Start Menu and Play again, the breakableCount doesn’t go back to 0 . despite the first line of code telling it breakableCount = 0

I added the line

Brick.breakableCount = 0;

into the script for my loseCollider, which fixed it, but pretty sure that’s not the best solution.
Here my brick.cs script,

The part where you’ve declared your breakable count to 0 only gets called when you start the game. If you lose, or otherwise load another level it does not get reset to 0.

It’s been a while since I did this section but I checked my code and I reset the breakable count when loading a new level. I think this was covered in one of the following lessons.

Also use breakableCount = GameObject.FindGameObjectsWithTag("Breakable").Length; instead of incrementing breakableCount variable.

3 Likes

Why use that line of code when breakableCount++ works just fine?

I never used he.lukasz’s suggestion. Mike was right, Ben addressed the error I was having in the later lesson to do with Debugging from User Comments (or something like that). To be honest I think i stuck with the solution I used anyway, which was to reset the count when a lose was triggered by the LoseCollider.

1 Like

I used your idea and it works fine now so thanks for that.

1 Like

I wrote about this line of code because it’s bad manner to count something if you have proper method already given to use. Sometimes it may cause an error. Error really hard to find in your code.

Thanks :slight_smile:

1 Like

I noticed this, too, and I reset the breakableBrick count in both LoadLevel and LoadNextLevel, just to cover my bases.

Noticed this error after completing this lesson and came to the discussions hoping I wasn’t the only one seeing it. Tried your solution and it works great. Wasn’t sure where the best place to reset the count was.

1 Like

Hi! I’m also using the line that you sugested (checking in the API documentation), but I don’t understand why Ben doesn’t recommend this solution, I think he says something related to that in the start of the video but I don’t get it (because my english is not that good, so I get lost pretty often xD). He says something about that using that will take more time or something like that. Can you help me with that? Thnx in advance! Cheers

I don’t remember what Ben said. I finished that part of course near 8 months ago. :slight_smile: But if you have any doubts - there are a lot of proper solutions. As always in programming. I wrote about this line of code because it’s much more convinient and just safer. I don’t know why Ben did it in other way. Maybe he didn’t think about this? It’s really common issue. But… If you did it on your own, I mean you were looking for better solution in documentation then it’s huuuge plus for you and your skills/education. It means you care about your code and you don’t follow blindly. Good luck. :slight_smile:

1 Like

I used “breakableCount = 0” in Awake part, just before the Start. Is that way has any disadvantage? I’m very new to programming, so can’t be sure.

He says this: > "You could count the objects that have the “breakable” tag, but when you do that is not so obvious, because of timing issues."
What he exactly means is beyond my understanding.

If you put Lukasz’ proposal in Start(), it gives you a proper result. On Unity 5, it will be executed for each brick object, and will always set breakableCount to the same value, which is the total number of bricks.

1 Like

Hey guys, chiming in to explain the issue at hand here, I think it’s very useful to understand what’s happening and why.

It all have to do with the fact that, in Unity, static variables are NOT wiped when changing scene. They persist throughout the whole game once declared.

So, the first time Unity reads the line:

in the first brick script, it checks if there’s such a variable. Of course, there isn’t. So it proceeds to create it (declaration) and to initialize it to 0.
When it gets to the second brick script, it checks if that variable exist, and the answer is yes, so it skips completely the declaration and the initialization.

This happens every other time, except the first, the line is executed, and this is why we need to reset the value to 0 at every scene change.

Thus the solution presented by Lukasz is the most efficient one: you skip a lot of code (the if (breakable) etc.), and you just need to use his line just once per scene, for example putting that line in the LevelManager.LoadLevel() and LevelManager.LoadNextLevel() methods.

Hope this helps to clarify the issue. :wink:

3 Likes

I think this needs to be set only when a new game is started as each brick decrements breakableCount when it is destroyed so no need to reinitialize it every scene. I sovled this by testing if the level being loaded is the first level and then setting breakableCount to zero.

Of course doing this for every level would still work.

Here is my solution:

public void LoadLevel(string name){

	Debug.Log ("New Level load: " + name);

    // If loading scene 0 (as defined in Build Settings) this means a new game is started
    // so reinitialise the breakableCount otherwise this variable will contain accumulated counts 
    // from previous games played. A new game is a new beginning for this variable. 
    if (Application.loadedLevel == 0) { Brick.breakableCount = 0; }

	Application.LoadLevel (name);
   
}
1 Like