Problem with "Detecting win condition"

Hi everyone, I have some problems with this lesson, and I need help. I can’t understand why my program is not working, because Debug program can’t find a mistakes…
On this screenshots you can see my script for bricks, and my unity console



And here my script too

using System.Collections;
using UnityEngine;

public class Brick3 : MonoBehaviour
{
    public Sprite[] hitSprites;
    public static int brickCount = 0;

    private LevelManager levelManager;
    private int timesHit;
    private bool isBreakble;

    // Use this for initialization
    void Start()
    {
        levelManager = GameObject.FindObjectOfType<LevelManager>();

        isBreakble = (this.tag == "breakble");
        if (isBreakble) {
            brickCount++;
            print(brickCount); 
        }
        timesHit = 0;
        levelManager = GameObject.FindObjectOfType<LevelManager>();

    }

    void OnCollisionEnter2D(Collision2D col)
    {
        bool isBreakble = (this.tag == "Breakble");
        if (isBreakble)
        {HandleHits();}
        else {}
    }

    void HandleHits() {
        {
            timesHit++;
            int maxHits = hitSprites.Length + 1;
            brickCount--;
            print(brickCount);
            if (timesHit == maxHits) { 
                brickCount--;
                print(brickCount);
                Destroy(gameObject);
            }
            else LoadSprites();
        }
    }

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

    // TODO remove this method wheb we actually win!
    void SimulateWin()
    {
        levelManager.LoadNextLevel();
    }

}

    indent preformatted text by 4 spaces

Hi Andrew,

From what I can see, the correct spelling for the tag in your case is “Breakble”, with a capital B, so you should change that on line 13 of your Brick3 script.

Next is your HandleHits function, where you should remove lines 40 and 41 (you only want the brickcount to decrement if the brick gets destroyed, not every time it gets hit). The HandlieHits function also has an extra pair of braces around it, but that shouldn’t change how it works.

Hopefully that helps.

Great, now its working)) Thank you very much)

1 Like

Privacy & Terms