Hi, guys. I was watching a tutorial, where it’s been said that to destroy an instance of a prefab (which is instantiated in another script named ProjectileScript.js) 5 seconds after it’s hit something you need to attach this script to your prefab:
#pragma strict
function Start () {
}
function Update () {
}
function OnCollisionEnter (collision : Collision){
Destroy(gameObject,5);
}
But it has returned this 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.
So after a lot of googling I wrote/copypasted this script:
#pragma strict
var targetScript;
var targetObj: Transform;
function Start () {
}
function Update () {
}
function OnCollisionEnter (collision : Collision){
var targetScript: ProjectileScript = targetObj.GetComponent(ProjectileScript);
Destroy(targetScript.instance,5);
print(collision.transform.name);
}
The question is: was the first script supposed to work and destroy only one instance, but not the whole object, or maybe it was possible only in previous versions of Unity?
Thanks for help!