Search code examples
c#unity-game-enginedestroy

creating a Breakout game in unity3d c#-bricks wont get destroyed


I have made a breakout game using unity tutorials and everything seemed to work well, but when I played it in the game mode there were some errors. So after I tried fixing the errors, the bricks wont get destroyed anymore. I tried undoing it, writing the code anew or even writing the same project anew but nothing works. What can I do?

These are the codes for my game which are exactly like the codes in the tutorial!

Paddle:

public class Paddle : MonoBehaviour {

 public float paddleSpeed = 1f;
 private Vector3 playerPos = new Vector3(0, -9f, 0);
 void Update ()
 {
     float xPos = transform.position.x +     (Input.GetAxis("Horizontal")*paddleSpeed);
     playerPos = new Vector3(Mathf.Clamp(xPos, -7.5f, 7.5f), -9f, 0f);
     transform.position = playerPos;
 }

}

Ball:

public class Ball : MonoBehaviour {

 private float ballInitialVelocity = 600f;
 private Rigidbody rb;
 private bool ballInPlay;

 void Awake ()
 {
     rb = GetComponent<Rigidbody>();
 }

 void Update ()
 {
 if(Input.GetButtonDown("Fire1") && ballInPlay==false)
     {
         transform.parent = null;
         ballInPlay = true;
         rb.isKinematic = false;
         rb.AddForce(new Vector3(ballInitialVelocity, ballInitialVelocity, 0));
     }
 }
}

GM:

using UnityEngine; using System.Collections;

public class deathZone : MonoBehaviour {

 void OnTriggerEnter(Collider col)
 {
     GM.instance.loseLife();
 }
}

public class GM : MonoBehaviour {

 public int Lives = 3;
 public int bricks = 16;
 public float resetDelay = 1f;
 public Text livesText;
 public GameObject gameOver;
 public GameObject bricksPrefab;
 public GameObject youWon;
 public GameObject paddle;
 public GameObject deathParticles;
 public static GM instance = null;
 private GameObject clonePaddle;
 void Start()
 {
     if (instance == null)
         instance = this;
     else if (instance != this)
         instance = null;
     setup();
 }
 public void setup()
 {
     clonePaddle = Instantiate(paddle, new Vector3(0, -9,0),  Quaternion.identity) as GameObject;
     Instantiate(bricksPrefab, new Vector3((float)18.5, (float)-61.14095, (float)238.4855), Quaternion.identity);
 }
 void checkGameOver()
 {
     if (bricks < 1)
     {
         youWon.SetActive(true);
         Time.timeScale = .25f;
         Invoke("Reset", resetDelay);
     }
     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, new Vector3(0, -9, 0), Quaternion.identity) as GameObject;
 }
 public void destroyBrick()
 {
     bricks--;
     checkGameOver();
 }
}

Bricks:

public class Bricks : MonoBehaviour {

public GameObject brickParticle;

 void OnCollisionEnter(Collision other)
 {
     Instantiate(brickParticle, transform.position, Quaternion.identity);
     GM.instance.destroyBrick();
     Destroy(gameObject);
 }
}

deathZone:

public class deathZone : MonoBehaviour {
void OnTriggerEnter(Collider col)
 {
     GM.instance.loseLife();
 }
}

Solution

  • In this tutorial you have to make sure all of the bricks GameObject have both the Bricks script and a BoxCollider components. Also the ball should have a Rigidbody and a SphereCollider components.

    An easy way to debug Collisions or Triggers is to simply use a Debug.Log("something"); as a first command in the OnCollisionEnter/OnTriggerEnter/... methods.