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

Danny White
Danny White
7,107 Points

Player (frog) no longer moves from keyboard input

After completing this tutorial section, everything seemingly works except for the Player (frog) movement. Working items include:

  • Space bar to start the game
  • Camera pan-out
  • Bird moving towards from and 'killing' it (particles and disappearance)

Here is my GameState.cs code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

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 to its game position
        followCamera.enabled = false;
    }

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

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

            // ...then start the game.
            StartGame();

        }

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

            // ...then end the game
            EndGame();

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

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

                // ...then reload the currently loaded scene.
                SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
            }

        }

    }

    private void StartGame() {

        // Set the game state
        gameStarted = true;

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

        // Allow the bird to move
        birdMovement.enabled = true;

        // Allow the camera to move
        followCamera.enabled = true;
    }

    private void EndGame() {

        // Set the game state
        gameStarted = false;

        // Show the game over text
        gameStateText.color = Color.white;
        gameStateText.text = "Game Over";

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

1 Answer

Oğuzhan Emre Özdoğan
Oğuzhan Emre Özdoğan
3,579 Points

Firstly, there is a typo in your code. playerHealth should be PlayerHealth in the following if method: Note that PlayerHealth is anoher C# script and letter cases matter, and in that if statement you are trying to call the alive variable in the PlayerHealth script.

        // If the player is no longer alive...
        if (PlayerHealth.alive == false) {

            // ...then end the game
            EndGame();

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

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

                // ...then reload the currently loaded scene.
                SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
            }

        }

    }

Secondly, in the StartGame method, you need to enable the player movement. Just like this:

private void StartGame() {

        // Set the game state
        gameStarted = true;

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

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

        // Allow the bird to move
        birdMovement.enabled = true;

        // Allow the camera to move
        followCamera.enabled = true;
    }

Hope this helps!

Danny White
Danny White
7,107 Points

Thanks! Appreciate the break-down.

The missing playerMovement.enabled = true; was the issue.

I get an error in the Console if I change if (playerHealth.alive == false) to if (PlayerHealth.alive == false). I think it's because I create a variable, named like the former (lowercase), at the top of the script: private PlayerHealth playerHealth;.

So, I was able to fix the issue by following your advice in the second block of code—the StartGame(). Thanks again!