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.
URGENT!!! I am creating Flappy bird for my school project. When I add pipes generator it works well, but when it comes to pipes destroying, pipe generator just stopped working!
It says "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." Here are my two scripts using UnityEngine; using System.Collections;
public class Pipes : MonoBehaviour {
public GameObject thepipes;
public float maxheight;
public float minheight;
public float DistanceBetween;
public Transform generatorThreshold;
// Use this for initialization
void Start () {
transform.position = new Vector3 (transform.position.x + DistanceBetween, transform.position.y, transform .position.z);
}
// Update is called once per frame
void Update () {
if(transform .position. x <generatorThreshold.position.x)
{
Instantiate (thepipes, new Vector3(transform .position .x, Random.Range (minheight,maxheight),transform.position.z),transform .rotation);
transform.position = new Vector3 (transform.position.x + DistanceBetween, transform.position.y, transform .position.z);
}
}
}
And this is the another one..
using UnityEngine;
using System.Collections;
public class Destroy : MonoBehaviour {
public Transform destructionPoint;
// Use this for initialization
void Start () {
destructionPoint = GameObject.Find ("Destruction Point").transform;
}
// Update is called once per frame
void Update () {
if (transform.position.x < destructionPoint .position.x)
{
Destroy (gameObject);
}
}
}
It says I have to check it its null or not, but how??? Thanks!!
Answer by berkc · Apr 24, 2016 at 02:52 PM
Is Destroy class attached to each pipe gameobject? If so, use Destroy (this.gameObject);
to be sure. Otherwise I think this line causes a problem since "gameObject" is not defined in your class.
?? gameObject comes with every script. Try running Destroy(gameObject); yourself.