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

nicholasgryczewski
nicholasgryczewski
2,919 Points

It wont show me my serialized fields for game state on the inspector window.

here is my error message

Assets/gamestate.cs(19,13): error CS0246: The type or namespace name `PlayerHealth' could not be found. Are you missing an assembly reference?

here is my code

using System.Collections; 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 movement at the start of the game
    birdMovement.enabled = false;
    //prevent the follow camera fromm 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 the 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
        Scene.Manager.LoadScene(SceneManager.GetActiveScene().buildIndex);

    }
}


  private void StartGame() {
        //set game state
        gameStarted = true;
        //remove start text
        gameStateText.color = Color.clear;
        //allow player movement
        playerMovement.enabled = true;
        //allow bird movement
        birdMovement.enabled = true;
        //allow camera movement
        FollowCamera.enabled = true;


    }
      private void EndGame() {
        gameStarted = false;
        //show game over text
        gameStateText.color = color.white;
        gameStateText.text = "Game Over!";
        //Remove the player from game
        player.SetActive(false);

    }

}

what is the problem?

1 Answer

Damien Watson
Damien Watson
27,419 Points

Hey, The first thing to realise is that the inspector window doesn't update properly when there are errors in your code. Fix the errors and the field should appear.

I think the line with the error is :

private PlayerHealth playerHealth;

This creates the variable 'playerHealth' of type 'PlayerHealth'. Have you created the Class 'PlayerHealth' yet? If not, this is your error.

The PlayerHealth class should look something like this:

using UnityEngine;
using System.Collections;

public class PlayerHealth : MonoBehaviour {

    public bool alive;

    [SerializeField]
    private GameObject pickupPrefab;

    void Start () {
        alive = true;
    }

    void OnTriggerEnter(Collider other) {
        if (other.CompareTag("Enemy") && alive) {
            alive = false;

            Instantiate(pickupPrefab, transform.position, Quaternion.identity);
        }
    }
}

Let me know how you go.

Cheers, :Damien