- Home /
Missing Reference Exception
Hi All,
I've written a Spaceship Adventure game where enemies shoot a spaceship as the spaceship travels through the scene, one of my problems is when a bullet hits the enemy I destroy the gameObject and get the following error:
MissingReferenceException: The object of type 'AudioSource' 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. UnityEngine.AudioSource.Play ()
The way I have it setup is, my defense enemies have the script below attach to them. My understanding is if I destroy the object after a bullet hits it, I would assume this script wouldn't be called again? is that correct? because if this gets called again I would understand why I'm getting a "MissingReferenceException". Please help :)
using UnityEngine;
using System.Collections;
public class DefenseEnemy : MonoBehaviour {
private float lastUpdate;
private int[] shootingPositions = { 180, 205, 144 };
private float rotateDefenseTime = 1.0f;
private float whenToUpdateInSeconds = 2.0f;
private float bulletLife = 10.0f;
public GameObject bullet;
public float bulletSpeed = 1000.0f;
private float explotionLength = 1.0f;
//Enemy Health
private float health;
private bool dead = false;
public GameObject explotionPrefab;
//Audio
public AudioSource crashSound,shootSound;
void Start(){
health = 2.0f;
}
void Update () {
if(health == 0 || dead)
return;
//Every time change the Y rotation of the defense enemy
if(Time.time - lastUpdate >= whenToUpdateInSeconds && !dead){
//Shoot Bullet
iTween.ShakePosition(gameObject,new Vector3(0.4f,0.3f,0.4f),0.5f);
lastUpdate = Time.time;
iTween.RotateTo(gameObject,new Vector3(270,shootingPositions[Random.Range(0,3)],0),rotateDefenseTime);
ShootBullet();
}
}
void OnCollisionEnter(Collision col){
if(col.gameObject.tag == "SpaceshipBullet" && health > 0){
health--;
if(health == 0){
iTween.ShakePosition(gameObject,new Vector3(0.4f,0.3f,0.4f),0.5f);
crashSound.Play();
GameObject explotion = (GameObject)
Instantiate(explotionPrefab,gameObject.transform.position, Quaternion.identity);
dead = true;
Destroy(explotion,explotionLength);
Destroy(gameObject,explotionLength);
}
}
}
void ShootBullet(){
shootSound.Play();
GameObject newBullet = (GameObject)Instantiate(bullet,bullet.transform.position,bullet.transform.rotation);
newBullet.transform.localScale = new Vector3(0.12f,0.12f,0.12f);
newBullet.rigidbody.AddRelativeForce(Vector3.back * bulletSpeed);
Destroy(newBullet,bulletLife);
}
}
Answer by Xtro · Jul 29, 2013 at 08:01 PM
Disable the crashSound.Play() line and try it again. If you don't see the MissingReferenceException exception by doing so, your problem is the difference between the sound length and explosion length.
try to keep your explosion length longer than sound length.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
How to destroy a transform's parent object. 1 Answer
Coding Gui Text To Perform After GameObject Destroy? 1 Answer
GameObject reacts to audio source 1 Answer
Music works but not when accessed by another script in JavaScript 1 Answer