I need some assistance with the BlockBreaker game. The next level activates when I have destroyed only one brick. I think it is in the counting. It seems that the (breakCount --) doesn’t seem to work. I’m attaching my scripts for Blocks and Level Manager.
Thanks
Bill
Blocks Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Blocks : MonoBehaviour {
public AudioClip crack;
public Sprite[] hitSprites;
public static int breakableCount = 0;
private int TimesHit;
private LevelManager sceneManager;
private bool isBreakable;
// Use this for initialization
void Start () {
isBreakable = (this.tag == "Breakable");
//Here we keep track of the number of bricks in the game
if (isBreakable) {
breakableCount ++;
print (breakableCount);
}
TimesHit = 0;
sceneManager = GameObject.FindObjectOfType<LevelManager> ();
}
void OnCollisionExit2D (Collision2D Collision) {
AudioSource.PlayClipAtPoint (crack, transform.position);
if (isBreakable) {
HandleHits ();
}
}
void HandleHits (){
TimesHit++;
int maxHits = hitSprites.Length + 1;
if (TimesHit >= maxHits){
breakableCount --;
print (breakableCount);
sceneManager.BrickDestroyed();
Destroy(gameObject);
} else {
LoadSprites ();
}
}
void LoadSprites () {
int spriteIndex = TimesHit - 1;
if (hitSprites [spriteIndex]!= null) {
this.GetComponent<SpriteRenderer> ().sprite = hitSprites [spriteIndex];
}
}
void SimulateWin (){
SceneManager.LoadScene(SceneManager.GetActiveScene ().buildIndex + 1);
}
}
LevelManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelManager : MonoBehaviour {
public static int breakableCount;
public void LoadScene (string name) {
breakableCount = 0;
SceneManager.LoadScene(name);
}
// Update is called once per frame
public void QuitRequest () {
Application.Quit ();
}
public void LoadNextScene (){
breakableCount = 0;
SceneManager.LoadScene(SceneManager.GetActiveScene ().buildIndex + 1);
}
public void BrickDestroyed () {
if (breakableCount <= 0) {
LoadNextScene ();
}
}
}