Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity Breakout Game Ending Early

Discussion in 'Scripting' started by apie2546, Oct 9, 2015.

  1. apie2546

    apie2546

    Joined:
    Sep 13, 2015
    Posts:
    14
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class GM : MonoBehaviour {
    6.  
    7.     public int lives = 3;
    8.     public int bricks = 24;
    9.     public float resetDelay = 1f;
    10.     public Text livesText;
    11.     public GameObject gameOver;
    12.     public GameObject youWon;
    13.     public GameObject bricksPrefab;
    14.     public GameObject paddle;
    15.     public GameObject deathParticles;
    16.     public static GM instance = null;
    17.  
    18.     private GameObject clonePaddle;
    19.  
    20.     // Use this for initialization
    21.     void Awake ()
    22.     {
    23.         if (instance == null)
    24.             instance = this;
    25.         else if (instance != this)
    26.             Destroy (gameObject);
    27.    
    28.         Setup();
    29.    
    30.     }
    31.  
    32.     public void Setup()
    33.     {
    34.         clonePaddle = Instantiate(paddle, transform.position, Quaternion.identity) as GameObject;
    35.         Instantiate(bricksPrefab, transform.position, Quaternion.identity);
    36.     }
    37.  
    38.     void CheckGameOver()
    39.     {
    40.         if (bricks < 1)
    41.         {
    42.             youWon.SetActive(true);
    43.             Time.timeScale = .25f;
    44.             Invoke ("Reset", resetDelay);
    45.             Debug.Log ("You Won!");
    46.         }
    47.    
    48.         if (lives < 1)
    49.         {
    50.             gameOver.SetActive(true);
    51.             Time.timeScale = .25f;
    52.             Invoke ("Reset", resetDelay);
    53.         }
    54.    
    55.     }
    56.  
    57.     void Reset()
    58.     {
    59.         Time.timeScale = 1f;
    60.         Application.LoadLevel(Application.loadedLevel);
    61.     }
    62.  
    63.     public void LoseLife()
    64.     {
    65.         lives--;
    66.         livesText.text = "Lives: " + lives;
    67.         Instantiate(deathParticles, clonePaddle.transform.position, Quaternion.identity);
    68.         Destroy(clonePaddle);
    69.         Invoke ("SetupPaddle", resetDelay);
    70.         CheckGameOver();
    71.     }
    72.  
    73.     void SetupPaddle()
    74.     {
    75.         clonePaddle = Instantiate(paddle, transform.position, Quaternion.identity) as GameObject;
    76.     }
    77.  
    78.     public void DestroyBrick()
    79.     {
    80.         bricks--;
    81.         CheckGameOver();
    82.     }
    83. }
    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?
     
    Last edited: Oct 9, 2015
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Obviously you are calling DestroyBrick twice for every brick destroyed.

    The reason why is not in that code.
     
    Kiwasi likes this.
  3. apie2546

    apie2546

    Joined:
    Sep 13, 2015
    Posts:
    14
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Bricks : MonoBehaviour {
    5.    
    6.     public GameObject brickParticle;
    7.    
    8.     void OnCollisionEnter (Collision other)
    9.     {
    10.         Instantiate(brickParticle, transform.position, Quaternion.identity);
    11.         GM.instance.DestroyBrick();
    12.         Destroy(gameObject);
    13.  
    14.     }
    15. }
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  5. apie2546

    apie2546

    Joined:
    Sep 13, 2015
    Posts:
    14
    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
     
  6. apie2546

    apie2546

    Joined:
    Sep 13, 2015
    Posts:
    14
    Fixed it by giving each brick its on ID
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Bricks : MonoBehaviour {
    6.    
    7.     public GameObject brickParticle;
    8.     public static int count = 0;
    9.     public static List<string> destroyedBricks = new List<string> ();
    10.     public int ID;
    11.     void Start(){
    12.         ID = Bricks.count;
    13.         Bricks.count++;
    14.     }
    15.  
    16.     void OnCollisionEnter (Collision other)
    17.     {
    18.         if (destroyedBricks.Contains (ID.ToString ())) {
    19.  
    20.         } else {
    21.             destroyedBricks.Add(ID.ToString());
    22.             Instantiate (brickParticle, transform.position, Quaternion.identity);
    23.             GM.instance.DestroyBrick ();
    24.             Destroy (gameObject);
    25.             Debug.Log ("Brick Broken");
    26.         }
    27.  
    28.     }
    29. }
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    You should use GetInstanceId if you want a unique gameobject idenifier.