MissingReferenceException caused in coroutine
i am making a tile based game similar to that of 1010 as a test of my skills and as a learning opportunity, now, so far the project has been flawless until this moment, i current get the error :
"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."
after around 10-15 objects are placed or sometimes when the game first runs it will produce this error on all the items currently sitting in the dock. as indicated by the red line
my code currently as its stands is
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ObjectControl : MonoBehaviour {
private Vector3 startPoint;
private Vector3 target;
private Vector3 originalScale;
private RaycastHit Hit;
private bool hitCheck;
private bool sizecheck;
private bool ChildCheckY;
private bool ChildCheckX;
private static List<GameObject> destroyList = new List<GameObject> ();
private static List<GameObject> shrinkList = new List<GameObject> ();
private GameObject[] tileList;
private GameObject[] subobj;
private GameObject destroyitem;
private GameObject gameobj;
public float outline = 0.1f;
void Update(){
StartCoroutine (Scale());
}
void OnMouseDown()
{
originalScale = transform.localScale;
startPoint = transform.position;
gameObject.transform.localScale = Vector3.one;
}
void OnMouseDrag()
{
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.y = 1;
transform.position = target;
}
void OnMouseUp(){
subobj = new GameObject[transform.childCount];
RaycastHit hit;
hitCheck = true;
for (int i = 0; i < transform.childCount; i++) {
subobj [i] = transform.GetChild (i).gameObject;
}
foreach (GameObject child in subobj){
if (Physics.Raycast (child.transform.position, Vector3.down, out hit)) {
if (hit.collider.tag != "Tile")
hitCheck = false;
} else {
hitCheck = false;
}
}
if (hitCheck) {
foreach (GameObject child in subobj) {
Physics.Raycast (child.transform.position, Vector3.down, out hit);
child.transform.position = hit.transform.position;
child.transform.parent = hit.transform;
GameObject.Find("Spawns").GetComponent<CreatingItems>().score += 1f;
}
transform.parent = null;
GameObject.Destroy (this.gameObject);
} else {
transform.position = startPoint;
transform.localScale = originalScale;
}
if (GameObject.Find ("Spawn1").transform.childCount == 0 && GameObject.Find ("Spawn2").transform.childCount == 0 && GameObject.Find ("Spawn3").transform.childCount == 0) {
CreatingItems ci = GameObject.Find ("Spawns").GetComponent<CreatingItems> ();
ci.Spawn ();
}
tileList = new GameObject[GameObject.Find ("Generated Map").transform.childCount];
for (int i = 0; i < GameObject.Find ("Generated Map").transform.childCount; i++) {
tileList [i] = GameObject.Find ("Generated Map").transform.GetChild (i).gameObject;
}
for (int i = 0; i <= 9; i++) {
ChildCheckY = true;
ChildCheckX = true;
for (int y = 0; y <= 9; y++) {
if (tileList [(i*10) + y].transform.childCount == 0) {
ChildCheckY = false;
}
}
for (int x = 0; x <= 9; x++) {
if (tileList[i + (10*x)].transform.childCount == 0) {
ChildCheckX = false;
}
}
if (ChildCheckY) {
for (int y = 0; y <= 9; y++) {
shrinkList.Add(tileList [(i * 10) + y].transform.GetChild (0).gameObject);
}
}
if (ChildCheckX) {
for (int x = 0; x <= 9; x++) {
shrinkList.Add(tileList [i + (10 * x)].transform.GetChild (0).gameObject);
}
}
}
}
IEnumerator Scale(){
float speed = GameObject.Find ("Spawns").GetComponent<CreatingItems> ().deletespeed;
if (shrinkList.Count == 0) {
if (destroyList.Count != 0) {
for (int i = 0; i < destroyList.Count; i++) {
Destroy (destroyList [i]);
}
}
destroyList.Clear ();
} else {
for (int i = 0; i < shrinkList.Count; i++) {
GameObject gameobj = shrinkList [i];
while (gameobj.transform.localScale.x > 0.1f) {
gameobj.transform.localScale -= new Vector3 (0.1f, 0.05f, 0.1f) * speed / 100;
yield return null;
}
gameobj.SetActive (false);
destroyList.Add (gameobj);
}
for (int d = 0; d < shrinkList.Count; d++) {
GameObject.Find ("Spawns").GetComponent<CreatingItems> ().score += 1f;
}
shrinkList.Clear ();
}
}
}
the error occurs indicates the gameobject has been destroyed thoughout the while loop in the coroutine at line 129. i have little knowledge of coroutines and know i am probably using them wrong however any help would be greatly appreciated !
Your answer
![](https://koobas.hobune.stream/wayback/20220612103114im_/https://answers.unity.com/themes/thub/images/avi.jpg)