• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
-1
Question by AlexNotTheLion · Dec 20, 2016 at 04:22 PM · coroutinesonmouseupdestroyed

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. alt text 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 !

dock.png (77.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

84 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnMouseUp script error CS0116 2 Answers

Problems while implementing Replay 0 Answers

Why my Coroutine does unwanted pauses? 1 Answer

How to keep program linear and synchronous while getting user input? 1 Answer

Click two buttons at the same time 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges