Bricks are not being destroyed

As soon as I added music file to ball object , the bricks are not being destroyed. Help me in this regard

using UnityEngine;
using System.Collections;

public class Brick : MonoBehaviour {

public Sprite[] hitsprites;
public static int breakablecount=0;

bool IsBreakable;
private int timeshit;
private LevelManager levelManager;
// Use this for initialization
void Start () {

	IsBreakable = (this.tag== "Breakable");
	if (IsBreakable) {
	timeshit = 0;

// Update is called once per frame
void Update () {
void OnCollisionEnter2D(Collision2D collision){

	if (IsBreakable) {
		handlehits ();

void handlehits(){

	int maxHits = hitsprites.Length + 1;
	if (timeshit >= maxHits) {
		Destroy (gameObject);
	} else {
void LoadSprites(){
	int spriteindex = timeshit - 1;
	if (hitsprites [spriteindex]) {
		this.GetComponent<SpriteRenderer> ().sprite = hitsprites [spriteindex];
void Simulatewin (){
	levelManager.LoadNextLevel ();

and I have added an image from console.

What is your “timeshit” var for? Looks like it never changes from 0 aswell, so you’d never call the destroy gameobject code.

1 Like

In your handlehits() method you are missing the following’


Note, the case of the above is slightly different from your own variable, so correct either accordingly.

You are receiving the error message because when you call the LoadSprites() method, your timeshit variable still equals zero, at which point you are deducting 1 from it and then using that as the index in the hitsprites[] array.

This would be the same as saying “get me the sprite at position minus one”. As the array is zero based, it’s index starts at zero, hence the IndexOutOfRangeException error message.

Also, C# is case-sensitive and you would be wise to follow some convention with regards to the naming of variables and methods as you will find it more difficult later on when you come to debugging your code, equally, names may mean different things to different people.

For example, I’m reasonably confident that @Lucas_Ord read your timeshit variable as two words which were different from “times hit” :poop: :slight_smile:

I’d recommend sticking to camelCase for variables names at and PascalCase for method names, this will align with the course materials, examples you may find online and also make things a little easier for you and others to read. Above all, be consistent, at the moment you have a bit of a mixture in there.

Variables example;

 timeshit, levelManager, IsBreakable

That’s lowercase, camelCase and PascalCase all being used, where-as the following is a lot easier to follow;

timesHit, levelManager, isBreakable

Methods example;

Start(), Update(), OnCollisionEnter2D(), handlehits()

That’s PascalCase, PascalCase, PascalCase and then lowercase, where-as the following is easier to follow;

Start(), Update(), OnCollisionEnter2D(), HandleHits()

It may sound really petty, but hand-on-heart, being consistent and using some existing conventions here will really help :slight_smile:

See also;

1 Like

Privacy & Terms