- Home /
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!
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
Please include the full error as the numbers and file will tell us on what line the error is happening.
Your answer
![](https://koobas.hobune.stream/wayback/20220612054637im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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