- Home /
What is wrong with this script?
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!
Answer by sath · Jan 08, 2014 at 02:33 PM
Your first script if is attached to your prefab (which you want to destroy) is working only at the time you instatiate the prefab from your ProjectileScript. The error you have means that the script on prefab (the one you are showing us) is working perfect but the other script (ProjectileScript : you are not showing us) is trying to do something with this prefab (which is destroyed). Find what is that or give us and the other script so we can have a better "look" to can help you..
Thanks for response! Here's the script:
#pragma strict
var bullet : GameObject;
var fireRate : float = 0.5;
var timeTracker : float = 1;
function Start () {
}
var instance : GameObject;
function Update () {
timeTracker+=1*Time.deltaTime;
if(Input.GetButton("Fire1") && timeTracker>fireRate){
timeTracker=0;
instance = Instantiate(bullet, transform.position, transform.rotation);
instance.rigidbody.velocity = transform.TransformDirection(Vector3(0,0,20));
}
}
Looks like it did not work correctly because ins$$anonymous$$d of dragging the prefab into the "bullet" object I dragged the prefab into Hierarchy view, and from there into the "bullet" object. So that means that ins$$anonymous$$d of using the prefab I used it's instance (and then instantiated instances of an instance), am I right?
exactly !!!
but better make your script like this:
#pragma strict
var bullet : GameObject;//assign the prefab from Assets folder not from Hierarchy
var fireRate : float = 0.5;
var timeTracker : float = 1;
private var instance : GameObject;//hide from Inspector
function Update () {
timeTracker+=1*Time.deltaTime;
if(Input.GetButton("Fire1") && timeTracker>fireRate){
timeTracker=0;
instance = Instantiate(bullet, transform.position, transform.rotation);
if(instance){//if instance really exist (it is not null)
instance.rigidbody.velocity = transform.TransformDirection(Vector3(0,0,20));
}
}
}
Answer by Jessy · Jan 08, 2014 at 01:39 PM
These
Destroy(gameObject,5);
Destroy(targetScript.instance,5);
are only equivalent if the Game Object that the script is attached to is also the Game Object that targetScript.instance is attached to.
If you destroy a Game Object, you can't reference it anymore.
Thanks, but the question is why am I getting this error, and is there a more simple solution to this problem?
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
destroying game object in a specific transform 2 Answers
Alternatives to Instantiate/Destroy 1 Answer
Destroy a gameobject instance but also save it in a new variable 1 Answer
Instantiated GameObject being deleted when destroying original 1 Answer