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. grid.objGridSize () (at Assets/scripts/grid.cs:119) grid.hit (System.String hitObjString) (at Assets/scripts/grid.cs:109) grid.Update () (at Assets/scripts/grid.cs:80)
I'm getting this error and strange results after finding a gameobject, storing it in a new GameObject variable and then destroying it:
void Update () {
if(Input.GetButtonDown("Fire1")){ // detect left mouse click
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.Log("clicked");
if(Physics.Raycast(ray, out hitObj)){
if(hitObj.collider != null){
// send a message to the object that got hit
hit(hitObj.collider.gameObject.name);
}
}
}
}
public void hit(String hitObjString){
Debug.Log("I got hit: "+name);
GameObject hitTile = GameObject.Find(hitObjString);
if( hitTile.tag == "tile"){
Destroy(hitTile);
}
}