Bricks breaking on first hit regardless of increased timesHit;

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

public class Bricks : MonoBehaviour {

private LevelManager levelManager;
public int timesHit;
private int maxHits;
 

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

// Update is called once per frame
void Update() {
}

void OnCollisionEnter2D(Collision2D col) {
    timesHit++;

    if (timesHit >= maxHits) {
        Destroy(gameObject);
    }
  }
}

Iā€™ve double checked that my block prefabs have the ā€˜Brick scriptā€™ attached so I figured iā€™m doing something wrong on the script end of things. Anyone notice something iā€™m not on my script?

Hi Kyle,

There is a small mistake in your code. You need to make the variable ā€œmaxHitsā€ public, and increase its value. Currently it is not initialized, which means it is probably set to 0, so when you check if timesHit is larger than maxHits, you always have true.

For this game timesHit should not be set by the game designer but by the script, while maxHits on the other hand should be made accessible to the game designer in order to change the brick resistance.

I hope this helps! Enjoy the rest of the game! :slight_smile:

Always the little mistakesā€¦ It makes us humbleā€¦ Thank you @IanCher

1 Like

Typical of programming, and not only game programming! :wink:

1 Like

Privacy & Terms