Hi,
I tried to extend the Breakout Game and it worked pretty well so far. But now I’m working on a Pause Function and it causes me headaches. I finally got a solution working, but for some reason, this works only until you loose your first life.
public void Setup ()
{
clonePaddle = Instantiate (paddle, transform.position, Quaternion.Euler(0f,-180f,0f)) as GameObject;
Instantiate (bricksPrefab, transform.position, Quaternion.Euler(0f,-180f, 0f));
GameObject ballObject = GameObject.Find ("Ball");
ballScript = ballObject.GetComponent<Ball>();
Cursor.visible = false;
livesUI.SetActive (true);
So this function is called when the Player hits the Play button. It Instantiates the Paddle Prefab as a GameObject and I find a reference to it’s child the Ball.
public void Pause ()
{
paused = true;
pMScript.PauseGame ();
livesUI.SetActive (false);
Cursor.visible = true;
ballScript.Pause ();
}
public void Unpause ()
{
paused = false;
livesUI.SetActive (true);
ballScript.WakeUp ();
Cursor.visible = false;
}
With these functions and the reference I was able to call the two ballScript functions which are attached to a Script on the Ball prefab.
But when the Ball hits the ground the player loses a life, the paddle and his child gets destroyed and a after a short pause it gets instantiated
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.Euler(0f,-180f,0f)) as GameObject;
GameObject ballObject = GameObject.Find ("Ball");
ballScript = ballObject.GetComponent<Ball>();
I thought it would also destroy the reference and so I made a new one. But when I hit pause after loosing a life, my paddle stands still and the ball is sill moving and I have no clue why this is happeng.
Here is the Ball Script, I hope you guys can help me out with this.
public float ballInitialVelocity = 700f;
public Rigidbody rb;
private bool ballInPlay;
private Vector3 savedVelocity;
private Vector3 savedAngularVelocity;
// Use this for initialization
void Awake ()
{
rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown ("Fire1") && ballInPlay == false && GameManager.instance.paused == false)
{
transform.parent = null;
ballInPlay = true;
rb.isKinematic = false;
rb.AddForce (new Vector3 (ballInitialVelocity, ballInitialVelocity, 0));
}
}
public void Pause ()
{
savedVelocity = rb.velocity;
savedAngularVelocity = rb.angularVelocity;
rb.isKinematic = true;
}
public void WakeUp ()
{
rb.isKinematic = false;
rb.AddForce (savedVelocity, ForceMode.VelocityChange);
rb.AddTorque (savedAngularVelocity, ForceMode.VelocityChange);
}