Why on LoadNextLevel?

Just curious if there is something I have missed:
Under what circumstance is the code line “Brick.breakableCount = 0;” not redundant in the LoadNextLevel() method?
I belive in my code, breakableCount will always be 0 when that method is called, but I have not been doing this long so I want to be sure.

1 Like

I think this was a bit of a let’s be absolutely sure it’s reset kind of thing.

From what I remember there are two main methods for loading scenes, either you load the next playable scene which is called when all the bricks are destroyed, or, you call a scene by name, e.g. Start, Win, Lose.

If code is added as a precaution to clear a value, I would want to work through the possibilities of how it could ever be needed myself, and if it isn’t, then I would remove it. It may have been a left over from some testing during its production - best thing to do would be to perform some serious testing.

You could create a few playable scenes then turn on the autoplay, but set you code to repeat to the first scene again rather than the win scene, e.g. the game will play indefinitely, then use Debug.Log("Brick Count: " + Brick.breakableCount"); instead of Brick.breakableCount = 0; to see what is output to the console. You may want to create some unusual scenes where there’s the chance of the ball hitting more than one brick at the same time, and probably speed the whole game up a bit so you aren’t sat there until next Saturday watching it :slight_smile:

If you get anything above or below 0 you will then need to determine why it occurred.

Hope this helps :slight_smile:

I noticed the bug way before I got to the “bug report” part of the tutorial, so I tried to fix it myself. I only changed the “loading scene” method in level manager and have never noticed the bug since. I tried to trace when the LoadNextLevel() method is called, and it is called (afaik) only when the number of breakables on previous level is =< 0. Which mean that it would bug if the player destroyed more bricks then there was on the last level (negative number of breakables).

However, I thought it was bad practise to add suchs “just in case” bug fix? I mean, IF there was a bug that caused maps to end with a negative amount of breakables, would it not be better to let it bug out for real so you would notice the underlying bug rather then adding a superficial “in case there are bugs” fix?

Might be I am just picky ^o
^

1 Like

I would fully agree and yes :slight_smile:

I actually noticed this bug and addressed it prior to getting to the video that addresses it and did it differently.

I added the following to the brick.cs script. Is there a downside to the approach I took?
I get that its doing the same thing as the video did, but it makes more sense to me to do it within the brick script, I dunno why…

void Awake() {
breakableCount = 0;
}

Now I am very new to C# so would love to hear from someone a bit more experienced if my ‘fix’ was sensible and if not why…

Hi @Jack_Franklin,

Firstly, well done for experimenting and giving it a go yourself, best way to learn! :slight_smile:

I added the following to the brick.cs script. Is there a downside to the approach I took?

There could be :slight_smile:

So, if you have this on the brick script, each brick that is generated will set this count back to zero.

Now, as all of the bricks are generated at the start of a scene that probably isn’t a problem, although perhaps a little overkill, e.g. why have 200 bricks all say “our count is zero” - and on that very note, shouldn’t they actually be saying “I’m a brick, count me” - semantics etc.

I suspect the way that you are getting around it at the moment is because of the line of code which says that the breakableCount = the count of everything with a specific tag which fires after all of the bricks on in the scene.

So, it works, but it may lead to problems…

I will give you an example…

Perhaps you add a timer to a scene full of bricks, the idea being that its a “Time Till Next Brick” counter… so the player is busily destroying bricks as fast as they can, the counter is counting down, then hits zero - boom - new brick please… you spawn on one the screen, as you do, you set breakableCount back to zero, but there were still breakable blocks on the screen!

Again, you would most likely still get around this with the placement of the above line setting the count to equal however many have that tag. But I would suggest it may be a bit of a code smell to be repeatedly resetting how many bricks there are unnecessarily.

Hope my ramblings make some form of sense :slight_smile:

Hey Rob,

Thanks for taking the time to explain that. I think I perhaps had a misunderstanding of the Awake function. I thought it was called only once for the lifetime of the script, which lead me to think it would only be run at the start of each scene once.

If I’m understanding you, the Awake function is in fact run once per object that the script is attached to?

And so if we were to create further objects with the script attached half way through a level it would still run the awake function again for each new object created? Is that right?

1 Like

Hi Jack,

No worries… and yes, exactly…

To demonstrate the behaviour…

  • create a new scene
  • create an empty GameObject in the scene, rename it to GameController
  • add a new script component to the GameController GameObject, rename it to GameController.cs
  • create an empty GameObject in the scene, rename it to Jack
  • add a new script component to Jack, rename it to Jack.cs
  • drag the Jack GameObject out of the Hierarchy and into your Assets folder to create a prefab
  • remove the instance of the Jack prefab from the Hierarchy
  • replace the code within GameController.cs and Jack.cs with the following code respectively;

GameController.cs

using UnityEngine;

public class GameController : MonoBehaviour
{
    public Jack jack;

    /// <summary>
    /// Pre-Initialisation
    /// </summary>
    private void Awake()
    {
        Debug.Log("GameController is Awake");
    }

    /// <summary>
    /// Initialisation
    /// </summary>
    void Start()
    {
        Debug.Log("GameController has Started");
    }

    /// <summary>
    /// Update is called once per frame
    /// </summary>
    void Update()
    {

        if (Input.GetKeyDown(KeyCode.S))
        {
            Debug.Log("S Pressed");
            Instantiate(jack, gameObject.transform);    // create a new Jack!
        }

        Debug.Log("GameController has Updated");
    
}

Jack.cs

using UnityEngine;

public class Jack : MonoBehaviour
{
    /// <summary>
    /// Pre-Initialisation
    /// </summary>
    private void Awake()
    {
        Debug.Log("Jack is Awake");
    }

    /// <summary>
    /// Initialisation
    /// </summary>
    void Start()
    {
        Debug.Log("Jack has Started");
    }

    /// <summary>
    /// Update is called once per frame
    /// </summary>
    void Update()
    {
        Debug.Log("Jack has Updated");
    }
}

  • Drag the Jack prefab into the Jack field within the GameController script component on the GameController GameObject
  • Run the game
  • Note the entries in the log for Awake, Start and Update
  • The Update count should be getting reasonably high now, press the S key on the keyboard
  • Note the new entries in the log for Awake, Start and Update

Hope this helps.


See also;

FWIW i modified my lose collider script to restart the current level and if you don’t set breakable bricks to =0 then each re-load adds to those left over from the previous attempt at that level!

void OnTriggerEnter2D (Collider2D trigger) {
	//print ("trigger");
	Brick.breakableCount=0;	
	Application.LoadLevel(Application.loadedLevel);// reload curent level if lose
	//levelManager.LoadLevel ("Lose");
1 Like

This seems to be a solution providing you dont want to play again from the WinScreen.

I was thinking if a pause menu was added, it would probably be a scene so resetting the brick count at each load might not be optimal… Perhaps resetting when pressing “Play Again”