[SOLVED] Bricks seem to be duplicating and Level manager not loading properly

Hi all, Can I have a bit of help for my Blockbreaker please?

I am following Ben Tristem’s video on Udemy : Learn To Code By Making Games - The Complete Unity Developer.
I am now at Section 5 Lecture 89 “Blockbreaker : Automated Play Testing”. I have mostly followed all his examples except for where the functions have been outdated (yup! sorry! i’m late to the party!)

Everything was working as he showed before this video. But after I tried to insert my own code, I messed up somewhere and cant find where I did.

The problem encountered :

  1. The bricks seem to be duplicating on the 2nd or 3rd game test play.
  2. Sometimes the Win level doesn’t load when all the bricks are destroyed, or sometimes the next level loads even when there are still undestroyed bricks.

I include below the scripts for where the problem probably exists. Hoping better trained eyes can spot where the problem is!

[LEVEL MANAGER SCRIPT]

using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;

public class LevelManager : MonoBehaviour {

public void LoadLevel (string name) {
    SceneManager.LoadScene (name);
}


public void QuitLevel () {
    Application.Quit ();
}


public static void BrickDestroyed () {
	if (SceneManager.GetActiveScene ().name == "Level_03" & Brick.breakableCount <= 0) {
			SceneManager.LoadScene("Win");
	} else {
		LoadNextLevel ();
	}
}

     
public static void LoadNextLevel() {
	SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}   

}

[BRICK SCRIPT]

using UnityEngine;
using System.Collections;

public class Brick : MonoBehaviour {

public Sprite[] hitSprites;
public static int breakableCount;
public AudioClip crack;

private int timesHit;
private LevelManager levelManager;
private bool isBreakable;


void Start () {
    isBreakable = (this.tag == "Breakable");
    
    if (isBreakable) {
        breakableCount++;
	}

    timesHit = 0;
    levelManager = GameObject.FindObjectOfType<LevelManager>();
}


void OnCollisionEnter2D(Collision2D col) {
	
	AudioSource.PlayClipAtPoint (crack, transform.position);
    if (isBreakable) {        
        HandleHits();
    }

}


void HandleHits () {
    timesHit++;
    int maxHits = hitSprites.Length + 1;  
    
    if (timesHit >= maxHits) {
        breakableCount--;
        LevelManager.BrickDestroyed ();
        Destroy(gameObject); 
    } else {
        LoadSprites();
    }
}


void LoadSprites() {
    int spriteIndex = timesHit - 1;
	if (hitSprites[spriteIndex]) {
        this.GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
    }
}


private static void SimulateWin() {
    LevelManager.BrickDestroyed();
}  

}

THANK YOU THIS FAR! :slight_smile:

I might be wrong, but it looks like as soon as the first brick is destroyed, it will call LevelManager.BrickDestroyed(), which will straight away load the next level due to the structure of your if statement in BrickDestroyed().

e.g. you’re on Level_01, so SceneManager.GetActiveScene ().name == "Level_03" returns false, so the else is evaluated. It then calls LoadNextLevel().

Be careful also using the unary & vs the short-circuit &&. In this case it doesn’t make a difference, but it’s a really tough bug to track down. You’ll mostly want && unless you need & for a specific reason.

Taking your perspective, I’ve added the condition to the LoadNextLevel() as well. So guess I need more study on multiple if statement conditions.

public static void LoadNextLevel() {
if (Brick.breakableCount <= 0) {
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}

Since symptoms of the first problem are eliminated, it was easier to check up on the other one. Seems brick count is accumulating everytime player loses the game, so I’ve just added clause to reset count for every time Win or Lose.

Brick.breakableCount = 0;

So thanks a lot to ninjachimp for the help ! :slight_smile:

1 Like