Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by CK_KoopaTroopa · Jan 02, 2016 at 08:49 AM · instantiatemissingreferenceexceptiondestroy-clones

MissingReferenceException: The object of type 'laser' has been destroyed but you are still trying to access it.

Ok so lots and lots of instantiations are happening here... I'm making a 2D Star Wars themed game, and I have a lightsaber that is spawning drones (at random locations). The drones, then, are spawning lasers. I have it set so that the lasers stay on the screen until you deflect them, and then they are destroyed as soon as they hit an edge. Everything is working as I want it to, but for some reason it still throws me this error whenever I start or stop the program. Here's the code that spawns the lasers, which lies in each instantiation of the drones:

 using UnityEngine;
 using System.Collections;

 public class spawnLaser : MonoBehaviour {
     public GameObject originalLaser;
     
     public float minWaitTime = 5f;
     public float maxWaitTime = 15f;
     
     void Start(){
         StartCoroutine(waitAndSpawn());
     }
     
     void spawn () {
         GameObject clone = Instantiate (originalLaser);
         clone.SetActive (true);
         clone.transform.position = transform.position;
     }
     
     IEnumerator waitAndSpawn(){
         while (true) {
             yield return new WaitForSeconds (Random.Range (minWaitTime, maxWaitTime));
             spawn ();
         }
     }
 }

And here's the code in each of the lasers (I've taken out some large, irrelevant chunks of code to make it easier to read)

 using UnityEngine;
 using System.Collections;
 
 public class laser : MonoBehaviour {
     public float speed;
     private float vertExtent;
     private float horzExtent;
     public GameObject self;
     public bool deflected;
     public bool destroyMe;
     
     void Awake () {
         deflected = false;
         destroyMe = false;
     }
 
     void Start () {
         vertExtent = Camera.main.orthographicSize;    
         horzExtent = vertExtent * Screen.width / Screen.height;
         rotate();
     }
     
     void OnCollisionEnter2D (Collision2D col) {
         if (col.gameObject.name == "Lightsaber") {
             //code to make it deflect here
             deflected = true;
         }
     }
 
     void Update () {
         //code to make it move in the right direction here
         checkWalls ();
     }
 
     void rotate (){
         //a bunch of code went here
     }
 
     void checkWalls(){
         if (transform.position.x > horzExtent) {
             if(deflected){
                 destroyMe = true;
                 Destroy(self);
             } else {
                 transform.position = new Vector2(-horzExtent, transform.position.y);
             }
         }  //else if... for the other 3 walls
     }
 }

Like I said, it's not really affecting anything as far as I can tell, but I don't want that error there and I just can't seem to find the source of the problem!

Comment
Add comment · Show 2
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
avatar image ShadyProductions · Jan 02, 2016 at 01:09 PM 0
Share

try adding a check in your update method before you call checkWalls()

 if (!destroy$$anonymous$$e) //checks to see if the object isn't destroyed yet before it calls the method
avatar image maccabbe · Jan 02, 2016 at 03:31 PM 0
Share

Please include the full error as the numbers and file will tell us on what line the error is happening.

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

MissingReferenceException on other instances after destroying one. 1 Answer

How can I destroy many instantiated objects on endless game 1 Answer

When the player dies and I reload the scene the enemies spawn script stops working. 2 Answers

Deleting a clone when the clone is being made on another sprite 4 Answers

Destroy Instiantated GameObject on Collision2D 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