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

Akhil Kumar
Akhil Kumar
625 Points

Player Doesn't Die

Hey, so in the previous videos where there was no GameStateText, my player would die when the bird collided and proc'd the particle affects, but after this one, it doesnt ..

PS: Im new to Unity and this is my first course so i dont really know how to debug, would be great if there's a tutorial on that

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 = 3.5f;
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 player from moving
    playerMovement.enabled = false;

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

    // Prevent 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 user presses the spacebar ... 
    if (gameStarted == false && Input.GetKeyUp (KeyCode.Space)) {

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

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

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

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

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

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

    }
}

// Start the game
private void StartGame () {

    // Set game state
    gameStarted = true;

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

    // Enable movement
    playerMovement.enabled = true;
    birdMovement.enabled = true;
    followCamera.enabled = true;

}

// End the game
private void EndGame () {

    // Set game state
    gameStarted = false;

    // Show the GAME OVER text
    gameStateText.color = Color.red;
    gameStateText.text = "GAME OVER";

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

}

}

Can you show the class that decides when the player dies? Your Frog class looks fine to me.

I think the error is in the class that decides when the player should die. The class is probably Bird, but I don't see the full code, so I'm not sure.

Nathanial Harmon
Nathanial Harmon
505 Points

hi im also new, and having the same problem. In Unity, while playing the game, what error s the console at the bottom spitting out for you?

Nathanial Harmon
Nathanial Harmon
505 Points

i fixed my problem. the name of my script for player health was wrong/misspelled. (PlayerHaealth) when the script was created it put the class as PlayerHaealth. even though i corrected it in the code the name didn't change. only after changing the name of the script also did the script finally work. p.s. apparently doing this means you have to reatttach the script to the player with a drag and drop from the script fold for PlayerHealth and the PreFab PickupParticleDestruction. in the exact same manner as we did the Particles originally.