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