levelManager.BrickDestroyed() not calling on final breakable brick hit

Hi all,

I’ve been having issues with the last brick in my game to register loadlevel on final impact without needing to perform another impact on an invincible brick.
Basically the game keeps running after no bricks are left until the ball hits the static brick object which runs the collider2d again.
Is there a way to code the process so the LevelManager.BrickDestroyed() is not running on the subsequent collision?

I am using prefabs rather than updating sprites so my code is a bit different from the video course.
I threw around as many LevelManager.BrickDestroyed() and getCount for my prefabs as possible to see if it would fix it and it hasn’t, so there is redundancy in the code.
The way it works is I have a sequence of block prefabs that I assign a new prefab to upon collision after destroying the previous one and using the location of it to assign the next prefab.
The final prefab forms a gameobject that only has a transform component and a Tag labeled “Broken” whereas the other prefabs are labeled “Breakable”.
Brick.cs script attempts to count down the “Breakable” tags until 0 when levelManager.BrickDestroyed() should call.

I was trying to figure out a way to compare the “Breakable” decaying prefabs and the “Broken” generated prefabs so that there could be an equivalence at the end but don’t know how to setup the script with an array to do that sort of operation.
Any suggestions on how to get the collision to toggle the levelManager.BrickDestroyed() call without needing to hit another brick?

Thanks in advance.

BrickDestroyed() from LevelManager.cs

public void BrickDestroyed()
{
    if (Brick.broken == 0)
    {
        LoadNextLevel();
    }
} 

From Brick.cs

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;

public class Brick : MonoBehaviour {

public static int broken = -1;
private static int breakableCount = 0;
private LevelManager levelManager;
public Transform Prefabs;
private bool isBreakable;
private bool isBroken;
private GameObject[] getCount;
int compare;



// Use this for initialization
void Start () {
    isBreakable = (this.tag == "Breakable");
    if (isBreakable)
    {
        getCount = GameObject.FindGameObjectsWithTag("Breakable");
        broken = getCount.Length;
        print("b " + broken);
    }
    levelManager = GameObject.FindObjectOfType<LevelManager>();
}

// Update is called once per frame
void Update () {
    levelManager.BrickDestroyed();
}

private new Vector3 pos;
// when a collision happens
void OnCollisionEnter2D(Collision2D col)
{
    levelManager.BrickDestroyed();
    getCount = GameObject.FindGameObjectsWithTag("Breakable");
    broken = getCount.Length;
    print("b " + broken);
    levelManager.BrickDestroyed();
    if (isBreakable)
    {
        HandleHits();
    }
}
void HandleHits()
{
    levelManager.BrickDestroyed();
    pos = transform.position;
    Destroy(gameObject);
    Instantiate(Prefabs, pos, transform.rotation);
    getCount = GameObject.FindGameObjectsWithTag("Breakable");
    broken = getCount.Length;
    print("b " + broken);
    levelManager.BrickDestroyed();
}

Hi Lucky,

If I’m right, you MIGHT be using “broken” in much the same way “breakableCount” was intended to be used.

If so, try changing Brick to this (MAKE A BACKUP FIRST, JUST IN CASE):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Brick : MonoBehaviour {

    public static int broken = 0;
    private static int breakableCount = 0;
    private LevelManager levelManager;
    public Transform Prefabs;
    private bool isBreakable;
    private bool isBroken;
    private GameObject[] getCount;
    int compare;

    // Use this for initialization
    void Start () {
        isBreakable = (this.tag == "Breakable");
        if (isBreakable) {
            broken = broken + 1;
            print("b " + broken);
        }
        levelManager = GameObject.FindObjectOfType<LevelManager>();
    }

    void OnCollisionEnter2D(Collision2D col) {
        if (isBreakable) {
            HandleHits();
        }
    }

    void HandleHits(){
        Instantiate(Prefabs, pos, transform.rotation);
        Destroy(gameObject);
        broken = broken - 1;
        print("b " + broken);
        levelManager.BrickDestroyed();
    }
}

And change “levelManager.BrickDestroyed();” to:

public void BrickDestroyed() {
    if (Brick.broken <= 0) {
        LoadNextLevel();
    }
}

HOPEFULLY, this should work the same way…

Privacy & Terms