Hi all, Can I have a bit of help for my Blockbreaker please?
I am following Ben Tristem’s video on Udemy : Learn To Code By Making Games - The Complete Unity Developer.
I am now at Section 5 Lecture 89 “Blockbreaker : Automated Play Testing”. I have mostly followed all his examples except for where the functions have been outdated (yup! sorry! i’m late to the party!)
Everything was working as he showed before this video. But after I tried to insert my own code, I messed up somewhere and cant find where I did.
The problem encountered :
- The bricks seem to be duplicating on the 2nd or 3rd game test play.
- Sometimes the Win level doesn’t load when all the bricks are destroyed, or sometimes the next level loads even when there are still undestroyed bricks.
I include below the scripts for where the problem probably exists. Hoping better trained eyes can spot where the problem is!
[LEVEL MANAGER SCRIPT]
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class LevelManager : MonoBehaviour {
public void LoadLevel (string name) {
SceneManager.LoadScene (name);
}
public void QuitLevel () {
Application.Quit ();
}
public static void BrickDestroyed () {
if (SceneManager.GetActiveScene ().name == "Level_03" & Brick.breakableCount <= 0) {
SceneManager.LoadScene("Win");
} else {
LoadNextLevel ();
}
}
public static void LoadNextLevel() {
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
[BRICK SCRIPT]
using UnityEngine;
using System.Collections;
public class Brick : MonoBehaviour {
public Sprite[] hitSprites;
public static int breakableCount;
public AudioClip crack;
private int timesHit;
private LevelManager levelManager;
private bool isBreakable;
void Start () {
isBreakable = (this.tag == "Breakable");
if (isBreakable) {
breakableCount++;
}
timesHit = 0;
levelManager = GameObject.FindObjectOfType<LevelManager>();
}
void OnCollisionEnter2D(Collision2D col) {
AudioSource.PlayClipAtPoint (crack, transform.position);
if (isBreakable) {
HandleHits();
}
}
void HandleHits () {
timesHit++;
int maxHits = hitSprites.Length + 1;
if (timesHit >= maxHits) {
breakableCount--;
LevelManager.BrickDestroyed ();
Destroy(gameObject);
} else {
LoadSprites();
}
}
void LoadSprites() {
int spriteIndex = timesHit - 1;
if (hitSprites[spriteIndex]) {
this.GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
}
}
private static void SimulateWin() {
LevelManager.BrickDestroyed();
}
}
THANK YOU THIS FAR!