- Home /
missing object woe beginner
MissingReferenceException: The object of type 'Transform' 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.
Here, I want to make explosion but it throws such exception Before destroying object.
here is my code:
var shotSound: AudioClip; // drag a shot sound here, if any
// the PickupController component of the 'PickupSpawnPoints' GameObject
private var pickupController:PickupController;
public var explosion : Transform;
function Update(){
if (Input.GetKey(KeyCode.Space)) {
Shoot();
}
}
function Shoot(){
if (shotSound) audio.PlayOneShot(shotSound); // play the shot sound
var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray ,hit ,200))
{
Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
var pickupSpawnPoints:GameObject = hit.collider.gameObject;
var clone : Transform;
if(pickupSpawnPoints.tag == "Pickup")
{
clone = Instantiate(explosion,pickupSpawnPoints.transform.position, transform.rotation);
Destroy(clone.gameObject);
}
}
}
I am currently learning unity. Please help me to solve this error . Thanx for ur help in advance,..
clone = Instantiate(explosion,pickupSpawnPoints.transform.position, transform.rotation); // here i get error
Answer by AlucardJay · Feb 27, 2013 at 05:34 PM
You are destroying your object in the very same frame that you are instantiating it :
if(pickupSpawnPoints.tag == "Pickup")
{
clone = Instantiate(explosion,pickupSpawnPoints.transform.position, transform.rotation);
Destroy(clone.gameObject);
}
If you want to instantiate your particle prefab, let it complete its emission, then destroy it, you have 2 options.
First, look at the Unity Scripting Reference for Destroy : http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html
Note you can put a delay right there in the command :
// Kills the game object in 5 seconds after loading the object
Destroy (gameObject, 5);
the second option is to attach a script to your particle prefab :
#pragma strict
public var destroyTime : float = 10.0;
function Start()
{
Destroy( gameObject, destroyTime );
}
Pick one, and have fun !
Your answer
Follow this Question
Related Questions
Destroying and Loading Object Promblem? 1 Answer
A node in a childnode? 1 Answer
gun muzzle flashes 2 Answers
How do I destroy a specific clone of a GameObject? 1 Answer
Explosion Using Particle System 0 Answers