Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Game Development How to Make a Video Game Score, Enemies, and Game State Add a Start and End

Henrik Nord Aa Kristiansen
Henrik Nord Aa Kristiansen
815 Points

game state (start and stop the game) i have bin sitting with this for a long time plzz help

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 playerMove playerMovement;
private PlayerHealth playerHealth;

// Use this for initialization
void Start () {
    Cursor.visible = false;

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

    // privent 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 to moving to its start potition 
    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)) {

        //... 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 the restart
        restartTimer = restartTimer + Time.deltaTime;

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

            //...then reload the curent loadet sceen
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

        }
    }
}
private void StartGame() {

    //set the game state
    gameStarted = false;

    //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;
}

private void EndGame() {

    //set game state 
    gameStarted = false;

    //set the gametext to gameover
    gameStateText.color = Color.white;
    gameStateText.text = "Game Over!";

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

}

1 Answer

Sorry for late response.

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

should be

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