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 How to Make a Video Game Score, Enemies, and Game State Add a Start and End

T. Linklater
T. Linklater
412 Points

Code should be included!

I have errors and I am trying to cross reference the code your wrote but it is so long that I cannot see the entire text. It is next to impossible to see the text in its entirety just by scrubbing through the video.

<b>I suggest that you include a copy of the text that you write below each video.</b>

Secondly, it would be good to teach how to read the errors in the Console. I see that it tells me the issues but it is not clear to me what each one means. By adding a short video at the start explaining how to read the Console, it would be a huge help!

Overall, this series is excellent though. By adding these two fixes, it would make the couse much easier to follow along with.

Amy Shimoshige
Amy Shimoshige
1,826 Points

i agree! I find it helpful to compare my code when I have errors to know what I've done wrong, and the errors given by Unity often don't make a whole lot of sense to me.

1 Answer

jason chan
jason chan
31,009 Points

I would recommend follow along the video again and see where the syntax error is. Did you close your curly braces? Did you put semicolons in places that end a statement? Here's the code for gamestat file.

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

public class GameState : MonoBehaviour {

    private bool gameStarted = false;
    [SerializeField]
    private Text gameStateText;
    [SerializeField]
    private GameObject player;
    [SerializeField]
    private BirdMovement birdMovement;
    [SerializeField]
    private FollowCamera followCamera;
    private float restartDelay = 3f;
    private float restartTimer;
    private PlayerMovement playerMovement;
    private PlayerHealth playerHealth;

    // Use this for initialization
    void Start () {
        Cursor.visible = false;
        playerMovement = player.GetComponent<PlayerMovement> ();
        playerHealth = player.GetComponent<PlayerHealth> ();

        // prevent the player from moving at the start of the game
        playerMovement.enabled = false;
        // prevent the bird from moving at the start of the game
        birdMovement.enabled = false;
        // prevent the follow camera from moving game position
        followCamera.enabled = false;
    }

    // Update is called once per frame
    void Update () {
        if (gameStarted == false && Input.GetKeyUp (KeyCode.Space) || Input.GetKeyUp (KeyCode.Return) || Input.GetKeyUp (KeyCode.UpArrow)) {
            StartGame();
        }
        // if the player is no longer alive end game
        if (playerHealth.alive == false) {
            EndGame();

            //timer count up to restarting
            restartTimer = restartTimer + Time.deltaTime;
            // and if it reaches the restart delay..
            if (restartTimer >= restartDelay) {
                // ... then reload the currently loaded level
                Application.LoadLevel (Application.loadedLevel);
            }
        }
        if (Input.GetKey(KeyCode.Escape))
        {
            Application.Quit();
        }
    }
    private void StartGame() {
        // set the game state
        gameStarted = true;
        // remove the start text
        gameStateText.color = Color.clear;
        // allow the player to move
        playerMovement.enabled = true;
        birdMovement.enabled = true;
        followCamera.enabled = true;
    }
    private void EndGame() {
        gameStarted = false;
        // show the game over text
        gameStateText.color = Color.white;
        gameStateText.text = "You Lose!";
        // remove the player
        player.SetActive (false);
    }
}
T. Linklater
T. Linklater
412 Points

Thank you very much for the code, Jason. Very much appreciated! In fact, I did follow along with every step and created it myself. But there is times when an error occurs and when the code gets too long, it no longer can be shown on the screen in its entirety and you must scrub the timeline forward and back see all the code. So it became very inconvenient. But having this code in its entirety allows me to cross reference both codes side-by-side to see where any discrepancies have occurred.

Thanks again!