- Home /
Problem with my Gamepause Code
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 real clue why this is happening. But I guess it hase something to do with the reference to the Ball prefab. 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);
}
Problem solved
The Ball was spawned together with the paddle, but only the paddle got destroyed when the player lost a life. So I ended up with two Balls wich confused the script and the pause function.
Your answer
Follow this Question
Related Questions
How can i color a object material color using StartCoroutine ? 1 Answer
Why is my raycast projecting from the middle instead of the bottom? 0 Answers
Working with 2 scripts 2 Answers
How do I Instantiate a prefab on another game object's position (2D) 1 Answer
How can i highlight objects with the mouse when the mouse cursor is moving over objects ? 1 Answer