Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Neglli · Jun 10, 2016 at 09:06 PM · scripting problempausegame

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);
 
     }


Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Neglli · Jun 11, 2016 at 06:07 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges