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.