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

AFIR MOURAD TAHAR
AFIR MOURAD TAHAR
3,766 Points

Game development: Unity Engine fires errors, while I do exactly as instructed in the course.

Hi, I’m stuck while running the game. I’m using the Mono develop rev 5.9.6

The code is:

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 = GetComponent<PlayerMovement> ();
    playerHealth = GetComponent<PlayerHealth> ();

    //Prevent the player from moving
    playerMovement.enabled = false ;


    //Prevent bird from moving
    birdMovement.enabled = false;

    //Prevent camera from following
    followCamera.enabled = false;
   }

// Update is called once per frame
void Update () 
  {
    // if the game is not started and the user presses space bar. the game starts. 

    if (gameStarted == false && Input.getKeyUp (KeyCode.Space)) 

     {
        StartGame();
     }

    // if player health alive is false then end the game

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

    // increment time
    restartTimer = restartTimer + Time.deltaTime;

    // check if timer reached the restart delay then restart the game from scrach.
    if (restartTimer >= restartDelay) 

    {
        Application.LoadLevel(Application.loadedlevel);
    }

   }

private void StartGame() 
   {

    //set the game state
    gameStarted = true;

    // remove the start text by putting it transparent .
    gameStateText.color = Color.clear;

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

    // enable the predator . the bird to move
    birdMovement.enabled = true;

    //allow the camera to move. 
    // this gives a nice zomm out.
    followCamera.enabled = true;

   }

private void EndGame() 
    {
      // set the game state as stopped
      gameStarted =false;

      //show the text
      gameStateText.color = Color.white;

      // show GAME OVER !!
      gameStateText.text = "GAME OVER!";

      // remove the player from the game
       player.SetActive(false);
    }

}

I wrote the code exactly as the course. But When I run it, the unity engine is showing several error messages. I’m using Unity version: 5.3.1

NullReferenceException: Object reference not set to an instance of an object GameState.Start () (at Assets/Scripts/GameState.cs:31)

I have been googling the issue since a week. and I couldn't have an answer. even from the teacher, who directed me to the forum.

Can anyone help in this? is this common to Unity. ?

3 Answers

AFIR MOURAD TAHAR
AFIR MOURAD TAHAR
3,766 Points

I Even tried Unity 5.2.1 same as the one the instructor downloaded. it gives the same error.

Still not soved?....Hmm I'm not sure whats wrong with it looks fine to me

AFIR MOURAD TAHAR
AFIR MOURAD TAHAR
3,766 Points

I gave up the course because no answer was received, either from Forum or from the team. I switched to another course at Treehouse.

Try Downloading the files from the video... That Should do the trick! :)

Sorry for the Long Reply

Same...