x


Need help fixing an error

I have a script set up in my 2D game so if my player gets too close to a mine it explodes and spawns another object that can hurt the player. This is the script:

var targetObj : GameObject;
var dangerObj : GameObject;
var orbSpeedMultiplier : float = 5;
var proximity : float = 3;


function Update() {

    var dist = Vector3.Distance(targetObj.transform.position, transform.position);

    //check whether you are within target proximity
    if (dist < proximity) {
    var orb : GameObject = Instantiate(dangerObj, targetObj.transform.position, Random.rotation);
    orb.rigidbody.velocity = Random.insideUnitSphere.normalized * orbSpeedMultiplier;
Destroy(targetObj);
    }
    }

It works fine, if I get to close to the mine, it goes away and spawns a new orb that is scripted to hurt the player if it comes in contact with them. The problem is I get this error message for every update (so A LOT):

*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. miner.Update () (at Assets/miner.js:11) *

I'm still learning scripting so any help is greatly appreciated. I'm sure it's a small mistake that I'm just overlooking.

more ▼

asked Oct 26 '10 at 05:11 PM

dmpaskiet gravatar image

dmpaskiet
351 13 14 28

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

targetObj is declared at the top, as a public variable. I don't know if you've set that in the inspector, or if some other code sets it.

After Destroy(targetObj) is called, that variable still points to targetObj. Next time Update gets called, the first line references targetObj again. Unity is warning you that it's a reference to a destroyed object.

There are any number of ways you can fix this, but it depends on who sets targetObj and what it represents. For example, you could set it to null after destroying the object. Then, at the top of Update, you'd need to add logic to skip the proximity test if targetObj is already null. Or maybe you'd want to set it to point at some other target once the first one is gone.

more ▼

answered Oct 26 '10 at 06:39 PM

Bampf gravatar image

Bampf
5k 8 19 49

(comments are locked)
10|3000 characters needed characters left

You destroy targetObj. Therefore after the first time the if Block is executed there is no targetObj anymore. Consequently when you are calling

Vector3.Distance(targetObj.transform.position, transform.position);

you receive the 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.

It tells you exactly what's wrong

more ▼

answered Oct 26 '10 at 06:35 PM

lhk gravatar image

lhk
520 14 15 24

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1951
x496
x30

asked: Oct 26 '10 at 05:11 PM

Seen: 1802 times

Last Updated: Oct 26 '10 at 05:11 PM