Been prying at this code for an hour now. Game ends early before all bricks are destoryed saying you won. I used debug.log and found out the code is infact running checkgameover and saying it won GM Script Code (CSharp): using UnityEngine; using System.Collections; using UnityEngine.UI; public class GM : MonoBehaviour { public int lives = 3; public int bricks = 24; public float resetDelay = 1f; public Text livesText; public GameObject gameOver; public GameObject youWon; public GameObject bricksPrefab; public GameObject paddle; public GameObject deathParticles; public static GM instance = null; private GameObject clonePaddle; // Use this for initialization void Awake () { if (instance == null) instance = this; else if (instance != this) Destroy (gameObject); Setup(); } public void Setup() { clonePaddle = Instantiate(paddle, transform.position, Quaternion.identity) as GameObject; Instantiate(bricksPrefab, transform.position, Quaternion.identity); } void CheckGameOver() { if (bricks < 1) { youWon.SetActive(true); Time.timeScale = .25f; Invoke ("Reset", resetDelay); Debug.Log ("You Won!"); } if (lives < 1) { gameOver.SetActive(true); Time.timeScale = .25f; Invoke ("Reset", resetDelay); } } void Reset() { Time.timeScale = 1f; Application.LoadLevel(Application.loadedLevel); } public void LoseLife() { lives--; livesText.text = "Lives: " + lives; Instantiate(deathParticles, clonePaddle.transform.position, Quaternion.identity); Destroy(clonePaddle); Invoke ("SetupPaddle", resetDelay); CheckGameOver(); } void SetupPaddle() { clonePaddle = Instantiate(paddle, transform.position, Quaternion.identity) as GameObject; } public void DestroyBrick() { bricks--; CheckGameOver(); } } I did follow the breakout tutorial all the way. The rest of the code is exact. I do have 24 bricks instead of 20. Check the rest of the code at https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CB0QFjAAahUKEwiDwMzipbTIAhWI04AKHW8YDaE&url=https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/creating-a-breakout-game&usg=AFQjCNHx-vFnB9g-KRDiqumUltZD4eHtDw&sig2=ie4BmZX9aFGYUDSobW9SHw&bvm=bv.104615367,d.eXY EDIT: Just tested, it ends when the player hits 10 bricks EDIT AGAIN: Tested without fullscreen. Looks like it can't keep up when it adds to many, and subtracts them from bricks remaining. FINAL EDIT: It looks like doubling the bricks (IE adding 48 instead of 24) makes it work. Why though?
Obviously you are calling DestroyBrick twice for every brick destroyed. The reason why is not in that code.
I couldn't find anywhere where it would delete twice. From what I can see when it collides it grabs destroybrick from GM and then destroys the game object. This and the GM code are the only places where bricks are changed. I do appreciate the help Code (CSharp): using UnityEngine; using System.Collections; public class Bricks : MonoBehaviour { public GameObject brickParticle; void OnCollisionEnter (Collision other) { Instantiate(brickParticle, transform.position, Quaternion.identity); GM.instance.DestroyBrick(); Destroy(gameObject); } }
You don't happen to have the brick component added twice do you? Put a Debug statement inside the DestroyBrick method. Throw one brick in the scene. Drop the ball on it. See how many times it debugs.
I just checked, not sure why I didn't think to put my Debug in there but yes, it hit on brick and ran the destroy brick twice. I checked my brick prefab and theres only one on each space, so I'm not sure if there is an update issue or somewhere a brick is being deleted twice
Fixed it by giving each brick its on ID Code (CSharp): using UnityEngine; using System.Collections; using System.Collections.Generic; public class Bricks : MonoBehaviour { public GameObject brickParticle; public static int count = 0; public static List<string> destroyedBricks = new List<string> (); public int ID; void Start(){ ID = Bricks.count; Bricks.count++; } void OnCollisionEnter (Collision other) { if (destroyedBricks.Contains (ID.ToString ())) { } else { destroyedBricks.Add(ID.ToString()); Instantiate (brickParticle, transform.position, Quaternion.identity); GM.instance.DestroyBrick (); Destroy (gameObject); Debug.Log ("Brick Broken"); } } }