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

Mike B
Mike B
10,424 Points

Frog doesn't disappear after bird eats it

The Frog particle shows up but the frog doesn't disappear...and the bird is able to move before the spacebar is pressed.

using UnityEngine; using System.Collections; 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 = GetComponent<PlayerMovement> ();
    playerHealth = GetComponent<PlayerHealth> ();

    // Prevent player from moving before start of game
    playerMovement.enabled = false;

    // Prevent bird from moving before start of game
    birdMovement.enabled = false;

    // Prevent follow camera from moving before start of game
    followCamera.enabled = false;
}

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

    // if game is not started and player presses space bar or touches screen, start game
    if (gameStarted == false && Input.GetKeyUp(KeyCode.Space)) {
        StartGame();
    }

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

        //end the game
        EndGame();

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

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

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

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

    // Hide Text
    gameStateText.color = Color.clear;

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

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

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

private void EndGame() {
    // Set game state
    gameStarted = false;

    //Set Game Over text
    gameStateText.text = "Game Over";
    gameStateText.color = Color.white;

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

}

Mike B
Mike B
10,424 Points

I figured it out....I forgot to add "player." before the "GetComponent" in the player health and the player movement components.