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

Restart timer not working & causing errors since restart not triggered.

Here in my code there is that if statement for the restart timer. If I take the code out the game restarts when the frog dies. If I leave it in the game never restarts and in 3 seconds I just get an error from all the other scripts that are requiring the player which is now destroyed. The errors all say this:

MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.Transform.get_position () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineTransformBindings.gen.cs:26) FollowCamera.Update () (at Assets/Scripts/FollowCamera.cs:19)

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 = player.GetComponent<PlayerMovement> ();
        PlayerHealth = player.GetComponent<PlayerHealth> ();

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

        //prevent the brid from moving at the start of the game
        birdMovement.enabled = false;

        //prevent the camera from moving at the start of the game
        followCamera.enabled = false;


    }

    // Update is called once per frame
    void Update () {
        //If the game is not started and the player presses the space bar then start
        if(gameStarted == false  && Input.GetKeyUp(KeyCode.Space)) {
            StartGame();
        }
        //If player is no longer alive then end game
        if(PlayerHealth.alive == false) {

            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 level
                SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
            }
        }
    }
    private void StartGame(){

        //set game state
        gameStarted = true;
        //remove start text
        gameStateText.color = Color.clear;
        playerMovement.enabled = true;
        birdMovement.enabled = true;
        followCamera.enabled = true;


    }
    private void EndGame(){
        gameStarted = false;
        gameStateText.color = Color.white;
        gameStateText.text = "Game Over!";

        //remove player from game
        player.SetActive (false);
    }
}```

1 Answer

Clayton Gorman
Clayton Gorman
3,189 Points

Without being home and looking at my code. I see private PlayerHealth PlayerHealth at the top. Change it to PlayerHealth playerHealth. Don't forget to change it in your update() if statement too