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

Whenever the bird eats the frog and the game restarts, an error pops up saying that the object was destroyed.

Its really weird. The error is "MissingReferenceException: The object of type 'GameObject' 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. GameState.EndGame () (at Assets/Prefabs/GameState.cs:81) GameState.Update () (at Assets/Prefabs/GameState.cs:47) ". Does anyone know what this means, and if so how to fix this?

4 Answers

jason chan
jason chan
31,009 Points

Hey Connor,

That means those lines are written incorrectly. Please watch the videos and again. Plus write the code again. Make sure there are no typos.

jason chan
jason chan
31,009 Points
using UnityEngine;
using System.Collections;

public class Brick : MonoBehaviour {
    public int maxHits;
    public int timesHit;
    private LevelManager levelManager;
    // Use this for initialization
    void Start () {
        timesHit = 0;
        levelManager = GameObject.FindObjectOfType<LevelManager> ();
    }

    // Update is called once per frame
    void Update () {

    }
    void OnCollisionEnter2D(Collision2D col){
        timesHit++;
        if(timesHit >= maxHits){
        Destroy (gameObject);
        }
    }
    // TODO remove this method once we win
    void SimulateWin (){
        levelManager.LoadNextLevel ();
    }
}

I don't really know what to do with this. Besides, I've been trying to fix this for the past 2 hours. I've looked through the videos about 15 times now. The real problem is that the frog is completely gone. Could you please clarify what you mean?

jason chan
jason chan
31,009 Points

GameState.cs This is code for the file. I'm reading the error log.

yah, its not working. It says that everything on line 14 is incorrect.

jason chan
jason chan
31,009 Points

well. I'm using 5.1 maybe it's because your using unity 5.2 the course is taught in unity 5.1. Maybe downgrade.