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


aaron joseph
aaron joseph
1,294 Points


I made a game and i was messing around and now i cant click space to start the game i get this error but i dont think its the cause

NullReferenceException: Object reference not set to an instance of an object GameState.StartGame () (at Assets/Scripts/GameState.cs:79) GameState.Update () (at Assets/Scripts/GameState.cs:50)

here is my code

for game state

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

public class GameState : MonoBehaviour {

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

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

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

    //...prevent player from moving at start of game
    playerMovement.enabled = false;

    //.. prevent bird from moving at start of game
    birdMovment.enabled = false;

    //prevent follow camera from moving to its game posotion
    followCamera.enabled = false;


// Update is called once per frame
void Update () {

    // if the game is not starte and player presses the space bar..
    if(gameStarted == false && Input.GetKeyUp (KeyCode.Space)) {

        //... then start the game

    //if player is no longer alive.
    if (playerHealth.alive == false)  {

        //then end the game

        //..increment a time to count up to estarting
        restartTimer = restartTimer + Time.deltaTime;

        //... and if it reaches the restart delay
        if (restartTimer >= restartDelay)   {

            //.. then reload the curently loaded level




private void StartGame(){
    //Set game state
    gameStarted = true;

    //remove the start text
    gameStateText.color = Color.clear;

    //allow player to move
    playerMovement.enabled = true;

    //allow bird to move
    birdMovment.enabled = true;

   // allow follow camera
    followCamera.enabled = true;

private void EndGame() {

    //set game state
    gameStarted = false;

    // show game over tet
    gameStateText.color = Color.white;
    gameStateText.text = "Game over!";

    //remove the player from game



1 Answer

aaron joseph
aaron joseph
1,294 Points

Ok i figured it our when i was messing with setting i removed the game state text from game manager