Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development

Instant Game Over - Game Broken!

I've followed the instructions in this video twice... As soon as I press play it says Game Over.

For this code:

if (playerHealth.alive == false) {
        EndGame();

If I also check gameStarted == true && playerHealth.alive == false then I get the "Press Space to Start" message. However, if I press Space then I instantly see "Game Over!".

I've added several print statements to check various scripts to see that they are starting. It doesn't seem that PlayerHealth.Start is executing (to set alive to true). Also, it seems that Player is not active at the start of the game.

I'm really discouraged because everything worked perfect until I followed this video, and I can't figure out why nothing is working now :(

4 Answers

Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

Hi Russell,

Sorry this took me so long! I downloaded the project and figured it out. The fix was a little tricky to discover, but it's very simple. If you select the Game Manager game object in the Hierarchy, the Inspector should show the Game State script is attached.

On the Game State script component, you've associated the wrong prefab in the "Player" field. See this screenshot:

http://i.imgur.com/yfXz1HJ.png

If you click on it in your Unity project, you'll notice that it highlights the prefab from the Project window. This is not the same as the prefab instance that's in the Scene. So instead, you'll need to drag the "Player" game object from the Hierarchy, and drop it into the Player field on the Game State script component.

See 13:40 of the video Add a Start and End to see where I drag the Player game object from the Hierarchy to the "Player" field in the Inspector.

Once I did that in your project, it worked perfectly. I hope that helps!

Thanks Nick!!

I knew it had to be something along those lines!! I started using that little ◎ button to add components to Serialized Fields, and apparently that's not always the right way to find them :(

Edit: and THANK YOU for spending the extra time to help me fix this!!

Nick Pettit
Nick Pettit
Treehouse Teacher

No problem. :)

When you use the asset selection window, there should be two tabs in the upper left that allow you to choose from assets in the scene or assets in the Project - as you've seen in this case, that does make a difference!

Oh I didn't see those tabs / didn't realize that was the difference. Thanks for the additional explanation!

I'm excited to finish this course finally!

Thanks!

Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

Hi Russell,

Sorry you're having trouble. :( Can you post the entirety of your PlayerHealth and GameState scripts?

Thanks Nick

PlayerHealth.cs:

using UnityEngine;
using System.Collections;

public class PlayerHealth : MonoBehaviour {

    public bool alive;

    [SerializeField]
    private GameObject pickupPrefab;

    // Use this for initialization
    void Start () {
        alive = true;
    }

    void OnTriggerEnter(Collider other) {
        if (other.CompareTag("Enemy") && alive == true) {
            alive = false;

            Instantiate(pickupPrefab, transform.position, Quaternion.identity);
        }
    }
}

GameState.cs:

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

public class GameState : MonoBehaviour {

    [SerializeField]
    private Text gameStateText;
    [SerializeField]
    private GameObject player;
    [SerializeField]
    private BirdMovement birdMovement;
    [SerializeField]
    private FollowCamera followCamera;

    private PlayerMovement playerMovement;
    private PlayerHealth playerHealth;

    private bool gameStarted = false;

    private float restartDelay = 3f;
    private float restartTimer;

    // Use this for initialization
    void Start () {
        Cursor.visible = false;

        playerMovement = player.GetComponent<PlayerMovement>();
        playerHealth = player.GetComponent<PlayerHealth>();

        playerMovement.enabled = false;
        birdMovement.enabled = false;
        followCamera.enabled = false;
    }

    // Update is called once per frame
    void Update () {
        if (gameStarted == false && Input.GetKeyUp(KeyCode.Space)) {
            StartGame();
        }

        if (gameStarted == true && playerHealth.alive == false) {
            EndGame();

            restartTimer = restartTimer + Time.deltaTime;
            if (restartTimer >= restartDelay) {
                Application.LoadLevel(Application.loadedLevel);
            }
        }
    }

    private void StartGame() {
        gameStarted = true;

        gameStateText.color = Color.clear;

        player.SetActive(true);
        playerMovement.enabled = true;
        birdMovement.enabled = true;
        followCamera.enabled = true;
    }

    private void EndGame() {
        gameStarted = false;

        gameStateText.color = Color.white;
        gameStateText.text = "Game Over!";

        player.SetActive(false);
    }
}
Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

This all looks OK to me. Are you getting any kind of errors or warnings in the Console window? For example, when you start the game, do any errors appear then?

Because the code looks correct, my guess is that something isn't associated properly in the serialized fields in the Inspector.

No I'm not getting any warnings or errors.. I was doing some print debugging (and watching the console to see if the PlayerHealth Start method was being called, etc), and couldn't figure out what was going on.

Here are some screenshots of the serialized fields:

Player Health

I thought the Alive checkbox here was really weird... Why does it show up even though it's not a serialized field? Probably just because it's public? Also, it doesn't show up as checked, which I guess makes sense cause we don't set alive = true until the script is started.

GameState

Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

Hey Russell,

That all looks correct as well.

Alright... I know this is a bit of a pain, but can you zip up your entire project directory and post it here so I can download it and take a look? I try to only use that as a last resort, but so far, I can't figure out what's wrong.

Nick, thanks! That's very nice of you.

I don't see a place to upload files... I tried going to Workspaces, but there's a 10MB limit on the file uploads there. My entire project is 235MB. I tried zipping just the Assets directory and it's 158MB.

Nick Pettit
Nick Pettit
Treehouse Teacher

Hi Russell,

You would need to host this elsewhere and then post the link. A public Dropbox link tends works best for large files like this.

You would need to host this elsewhere and then post the link. A public Dropbox link tends works best for large files like this.

Ahhh gotcha.

Frogs and Logs (Dropbox)

in case that link gives you a 404, it says it's still uploading...

Nick, were you able to download the project from my Dropbox?