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

jacques wiese
jacques wiese
913 Points

After following the last two videos on creating the game start and end components, when I press run, movement occurs

I have completed the gamestate script which should have disabled all the player movement but when i go to test the game the follow camera zooms out and the player and enemy can instantly move even though the "press spacebar to start is still visible. when i press spacebar it reverts to clear. im going to post my code but i cant seem to figure out what is going wrong. hope someone can explain this for me.

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>();

    //we need to prevent the player from moving at the start of the game
    playerMovement.enabled = false;
    birdMovement.enabled = false;
    followCamera.enabled = false;
}

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

    //if the game is not started and the player presses the spacebar then start the game
    if (gameStarted == false && Input.GetKeyUp(KeyCode.Space))
    {
        StartGame();
    }
    //if player is no longer alive
    if (playerHealth.alive == false)
    {
        //end the game
        EndGame();

        //increment a timer to count up from restarting
        restartTimer = restartTimer + Time.deltaTime;

        //and if it reaches the restart delay
        if (restartTimer >= restartDelay)
        {
            //reload the currently loaded level
            Application.LoadLevel(Application.loadedLevel);
        }
    }//end endgame  if block


}

private void StartGame()
{
    //set game start 
    gameStarted = true;
    gameStateText.color = Color.clear;

    playerMovement.enabled = true;
    birdMovement.enabled = true;
    followCamera.enabled = true;

}

private void EndGame()
{

    gameStarted = false;

    //set game text to game over
    gameStateText.color = Color.white;
    gameStateText.text = "Game Over!";

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

}

jacques wiese
jacques wiese
913 Points

btw all components are assigned to their serialized fields

1 Answer

Daniel Sattler
Daniel Sattler
4,867 Points

In your Start () Method change

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

to

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

and you should be good to go ;-)