2D catch game HELP PLEASE

so first this is my first game and I have done essentials so please don’t hate because I already know this is going to seem stupid to some.
Basically I don’t want a timer in my game at all. I only want to get a game-over and then have my restart button show up by missing a single ball and not catching it in my basket.

this is my GameController script.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class GameController : MonoBehaviour {

public Camera cam;
public GameObject ball;
public GameObject gameOverText;
public GameObject restartButton;
public GameObject splashScreen;
public GameObject startButton;
public HatController hatController;

private float maxWidth;
private bool playing;

// Use this for initialization
void Start () {
if (cam == null) {
cam = Camera.main;
}
playing = false;
Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
float ballWidth = ball.renderer.bounds.extents.x;
maxWidth = targetWidth.x;

}
void FixedUpdate () {

}
public void StartGame () {
splashScreen.SetActive (false);
startButton.SetActive (false);
hatController.ToggleControl (true);
StartCoroutine (Spawn ());
}

IEnumerator Spawn () {
yield return new WaitForSeconds (2.0f);
playing = true;
while (true) {
Vector3 spawnPosition = new Vector3 (
Random.Range (-maxWidth, maxWidth),
transform.position.y,
0.0f
);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (ball, spawnPosition, spawnRotation);
yield return new WaitForSeconds (Random.Range (1.0f, 2.0f));
}
yield return new WaitForSeconds (2.0f);
gameOverText.SetActive (true);
yield return new WaitForSeconds (2.0f);
restartButton.SetActive (true);
}

}

this is my score script.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Score : MonoBehaviour {

public Text scoreText;
public int ballValue;

private int score;

// Use this for initialization
void Start () {
score = 0;
UpdateScore ();
}

void OnTriggerEnter2D () {
score += ballValue;
UpdateScore ();
}

void UpdateScore () {

scoreText.text = “Coins Collected:\n” + score;

}
}

my basket controller script

using UnityEngine;
using System.Collections;

public class HatController : MonoBehaviour {

public Camera cam;

private float maxWidth;
private bool canControl;

// Use this for initialization
void Start () {
if (cam == null) {
cam = Camera.main;
}
canControl = false;
Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
maxWidth = targetWidth.x;
}

// Update is called once per frame
void Update () {
if (canControl) {
Vector3 rawPosition = cam.ScreenToWorldPoint (Input.mousePosition);
Vector3 targetPosition = new Vector3 (rawPosition.x , 0.0f, 0.0f);
float targetWidth = Mathf.Clamp (targetPosition.x, -maxWidth, maxWidth);
targetPosition = new Vector3 (targetWidth, targetPosition.y, targetPosition.z);
rigidbody2D.MovePosition (targetPosition);
}
}
public void ToggleControl (bool toggle) {
canControl = toggle;
}
}

My Destroy object script

using UnityEngine;
using System.Collections;

public class DestroyObject : MonoBehaviour {

void OnTriggerEnter2D (Collider2D other) {
Destroy (other.gameObject);
}
}

and finally my restart button script.

using UnityEngine;
using System.Collections;

public class Restart : MonoBehaviour {
public void RestartGame () {
Application.LoadLevel (Application.loadedLevel);
}

}

Please please pleaseeeeee help me out.

ok ive had a quick look through, but this is ALOT of code to read through.
if I could make a suggestion, for the next time when you post code, please use the ‘insert > code’ option just above, this will make it alot easier for people to read.

ive not really worked through this tutorial through to completion, but, ill see if i can help at all. please bear in mind that your scripts might be slightly different, depending if you have downloaded the completed project from the asset store or worked through the tutorial. so a copy and paste will probably not work, just go through it step by step and see if it makes sense. I have commented alot to try and help.

removing the timer:
in the heirarchy of the game under the UI Elements, disable the Timer and the Timer Shadow, that will stop them showing on screen.

in the game controller script;
you could just stop the timer counting down, buy commenting out the entire FixedUpdate, have a look at what ive done in the game controller script, ive commented the bits ive changed for you to see.

using UnityEngine;
using System.Collections;

public class HT_GameController : MonoBehaviour {
  
    public Camera cam;
    public GameObject[] balls;
    //public float timeLeft;        - not using the timer any more
    //public GUIText timerText; - dont need the timer text to be displayed
    public GameObject gameOverText;
    public GameObject restartButton;
    public GameObject splashScreen;
    public GameObject startButton;
    public HT_HatController hatController;
  
    private float maxWidth;
    private bool counting;
    public bool gameOver; // just a flag to use instead of the timer in the Spawn() function game loop
  
    // Use this for initialization
    void Start () {
        if (cam == null) {
            cam = Camera.main;
        }
        Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
        Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
        float ballWidth = balls[0].renderer.bounds.extents.x;
        maxWidth = targetWidth.x - ballWidth;
        //timerText.text = "TIME LEFT:\n" + Mathf.RoundToInt (timeLeft);    - dont need to set the timer text, as we arent using it.
    }

    void FixedUpdate () {
        // - **just commented out this whole block of code as its dealing with counting down the timer, which we dont use any more **
        //if (counting) {  
            //timeLeft -= Time.deltaTime;
            //if (timeLeft < 0) {
            //    timeLeft = 0;
        //    }
            //timerText.text = "TIME LEFT:\n" + Mathf.RoundToInt (timeLeft);
    //    }
    }

    public void StartGame () {
        gameOver = false; // so when we start the game, we set the game over flag as false, as this function is called from the start button
        splashScreen.SetActive (false);
        startButton.SetActive (false);
        hatController.ToggleControl (true);
        StartCoroutine (Spawn ());
    }

    public IEnumerator Spawn () {
        yield return new WaitForSeconds (2.0f);
        counting = true;
        while (!gameOver) {
            GameObject ball = balls [Random.Range (0, balls.Length)];
            Vector3 spawnPosition = new Vector3 (
                transform.position.x + Random.Range (-maxWidth, maxWidth),
                transform.position.y,
                0.0f
            );
            Quaternion spawnRotation = Quaternion.identity;
            Instantiate (ball, spawnPosition, spawnRotation);
            yield return new WaitForSeconds (Random.Range (1.0f, 2.0f));
        }
        yield return new WaitForSeconds (2.0f);
        gameOverText.SetActive (true);
        yield return new WaitForSeconds (2.0f);
        restartButton.SetActive (true);
    }
}

one thing more to do, is to tell the game controller when and how we are going to end the game, seen as were not using a timer anymore.
so, after working through the tutorial you will have noticed the edge collider that is around the game area, this destroys the balls as they touch it. so, if we want to end the game if one of the ‘normal’ collectable balls then we could use this collider.
so we have to have a look at the destroy on contact script attached to the destroyer object.
bear in mind that this destroy on contact is also used within the very bottom of the hat gameobject.

so, we have to check that its the bowling ball thats collided, and that its hit the collider thats attached to the Destroyer gameobject. have a look at the destroyoncontact script below

using UnityEngine;
using System.Collections;

public class HT_DestroyOnContact : MonoBehaviour {

    // need this line in so we can drag the game controller onto this script and reference it
    public HT_GameController gameController;

    void OnTriggerEnter2D (Collider2D other) {

        // so when a collider touches this edge collider, it would normally destroy it.
        // but, we can also find out what the tag of the colliding object is.
        // so if its the normal BowlingBall and its touched the collider attached to the Destroyer game object
        //, then we can get the gameOver bool in the gamecontroller, and this will end the game
        // as it will exit the loop in the spawn function.

        if (other.tag != "Bomb" && gameObject.name=="Destroyer")
        {
            gameController.gameOver = true;
        }
        Destroy (other.gameObject);
    }
}

hope that gets you underway…

1 Like

Perfect. Just got off work and trying now. thank you so much for responding