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

Nathan Pennington
Nathan Pennington
298 Points

compiler error

private void EndGame() {

    // set the game state

    gameStarted = false;

    //show game over text

    gameStateText.color = Color.white;
    gameStateText.text = "Game Over!";

    // remove player from game

    player.SetActive (false);



}

the gameStateText.color and gameStateText.text cause an error

Joseph Frazer
Joseph Frazer
5,404 Points

Can you please post the error?

Nathan Pennington
Nathan Pennington
298 Points

Assets/Scripts/GameState.cs(127,17): error CS1061: Type UnityEngine.Texture' does not contain a definition forcolor' and no extension method color' of typeUnityEngine.Texture' could be found. Are you missing an assembly reference?

Seth Kroger
Seth Kroger
56,413 Points

The error is probably not in the snippet you posted but higher up in the script where you declare gameStateText. It looks like you declared it as an incorrect type.

Nathan Pennington
Nathan Pennington
298 Points

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 Texture 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 teh bird from moving at start

    birdMovement.enabled = false;

    //prevent follow camera from moving

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

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

    //Remove the start text

    gameStateText = 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 the game state

    gameStarted = false;

    //show game over text

    gameStateText.color = Color.white;
    gameStateText.text = "Game Over!";

    // remove player from game

    player.SetActive (false);



}

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

gameStateText should be declared as a Text object, not Texture. These are rather different things in game engines.

Nathan Pennington
Nathan Pennington
298 Points

Got it. Ok I fixed the gameStateText and now it says

Assets/Scripts/GameState.cs(98,25): error CS0029: Cannot implicitly convert type UnityEngine.Color' toUnityEngine.UI.Text'

Seth Kroger
Seth Kroger
56,413 Points

Look at the line indicated (line 98). Take a look at what you are trying to do and see if you should be using the Text or it's color.