• 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 GingerV1k1ng · Mar 06, 2013 at 01:34 PM · destroyrendererball

Why is my ball Destroyed?

I am making a breakout game, and when the player gets game over i just want the ball to not render and stop moving but for some reason it also destroys the ball, i really don't get it. The reason i don't want the ball to be destroyed is because it have some parts i want to keep in the game after you lose like a menu system and some music. Here is my script.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class BallScript : MonoBehaviour {
     public float multiplier = 1.1f;
     public float minSpeed = 15;
     public float maxSpeed = 20;
     
     public float curSpeed = 0;
     
     private Transform _t;
     
     public List<GameObject> blocks;
     
     private bool Menushown;
     
     void Awake()
     {
         _t = transform;
         
         blocks = new List<GameObject>();
     }
     // Use this for initialization
     void Start () 
     {
         blocks.AddRange(GameObject.FindGameObjectsWithTag("Block"));
         
         ResetBall();
         
         Menushown = false;
     }
     
     // Update is called once per frame
     void Update () 
     {
         if(blocks.Count < 1)
             WonLevel();
             
         if(_t.position.y < -4.5)
             MissedBall();
     }
     
     void FixedUpdate()
     {
         curSpeed = Vector3.Magnitude(rigidbody.velocity);
         
         if(curSpeed > maxSpeed)
             rigidbody.velocity /= curSpeed / maxSpeed;
         
         if(curSpeed < minSpeed && curSpeed > 0)
             rigidbody.velocity /= curSpeed / minSpeed;
         
     }
     
     void OnCollisionEnter(Collision col)
     {
         rigidbody.velocity += rigidbody.velocity * multiplier;
         
         if(col.gameObject.CompareTag("Block"))
             blocks.Remove(col.gameObject);
         
         Debug.LogWarning("Blocks Left: " + blocks.Count);
     }
 
      void MissedBall()
     {
         if(LiveScript.curLives -- < 2)
             GameOver();
         else
             ResetBall();        
     }
     
     private void ResetBall()
     {
         _t.position = new Vector3(-PlayerScript.xBoundry + 2,PlayerScript.xBoundry - 2,PlayerScript.zPosition);
         _t.LookAt(GameObject.FindGameObjectWithTag("Player").transform.position);
         
         rigidbody.AddRelativeForce(new Vector3(0,0,minSpeed));
     }
     
     
     private void GameOver()
     {            
         Debug.Log("GameOver");
         renderer.enabled = false;
         rigidbody.velocity = Vector3.zero;
         _t.position = Vector3.zero;
         Menushown = true;
         
     }
     
     private void WonLevel()
     {
         Debug.Log("We Won Level");
     }
     
     void OnGUI()
     {
         if(Menushown)
         {
             if (GUI.Button(new Rect(10, 70, 50, 30), "Click"))
             Debug.Log("Clicked the button with text");
         }
     }
 }
 
Comment
Add comment · Show 2
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 Landern · Mar 06, 2013 at 01:37 PM 0
Share

No where in the script do you destroy the ball, are you switching scenes and not retaining a reference to the ball?

avatar image GingerV1k1ng · Mar 06, 2013 at 01:48 PM 0
Share

That is the ting i don't and im so confused at the moment because of this...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ben-Stoneman · Mar 06, 2013 at 02:03 PM

You are disabling the mesh renderer but you are never re-enabling it.

Not sure if Resetball() does this, or whether it just resets the position.

That's where you want to look... In the mesh's renderer.enabled

Comment
Add comment · Show 1 · Share
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 GingerV1k1ng · Mar 06, 2013 at 02:06 PM 0
Share

Yea i kinda got that, but the thing is that i want it to not enable agian, you have 3 lives and when you hit 0 it should not render and it does that, but I don't get why it destroys itself.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer

help with tunnel Game 1 Answer

Remove object with specific object 1 Answer

How to show a scored goal? 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