- Home /
Cant Destroy specific object
I can call Destroy(gameObject) inside a game Objects onTriggerEnter function and it will delete the object correct object, however if I call it in the update function, it will delete a random object. I have tried to destroy(this) and a few other ways, I know that it must be something to do with needing an instance of the object or something.
EDIT (This script is attached to the gameObject I want to kill when a Global Var is true)
var nextTarget : GameObject;
var time : int;
var money : float;
var ship : GameObject;
var passenger : GameObject;
var setTask;
function OnTriggerEnter (other : Collider) {
var addTime = GameObject.Find("PassengerTime");
setTask = GameObject.Find("pointerY");
passenger = this.gameObject;
if(setTask.GetComponent(MasterTriggerScript).currentTarget == null)
{
setTask.GetComponent(MasterTriggerScript).currentTarget = nextTarget;
setTask.GetComponent(MasterTriggerScript).currentTargetMoney = money;
ship = other.gameObject ;
setTask.GetComponent(MasterTriggerScript).compTask = false;
setTask.GetComponent(MasterTriggerScript).Task = true;
addTime.GetComponent(CountDown).currentTime = time;
addTime.GetComponent(CountDown).Awake();
}
}
function KillMe(object:GameObject){
//this.enabled = false;
print("KILL ME");
Destroy(object);
}
function Update(){
setTask = GameObject.Find("pointerY");
// passenger = GameObject.Find("Passenger/Passenger/Skeleton/Bone");
if(ship)
{
this.gameObject.transform.position = ship.gameObject.transform.position;
this.gameObject.transform.rotation = ship.gameObject.transform.rotation;
this.gameObject.transform.Rotate(0,90,0);
}
if(setTask.GetComponent(MasterTriggerScript).compTask == true)
{
KillMe(passenger);
setTask.GetComponent(MasterTriggerScript).compTask = false;
}
}
Answer by aldonaletto · Mar 17, 2012 at 02:32 PM
Destroy(gameObject) is a suicide instruction: the object destroys itself, since gameObject alone is presumed to be the reference to the GameObject that has the script attached. If you call Destroy(gameObject) at Update, the object will live for only one Update - it will get destroyed before the next Update is called.
Edit your question and post your script, or we can't get any clue about what's causing your problem.
Answer by CC Inc · Mar 17, 2012 at 02:29 PM
I think you need to call Destroy(gameObject); inside a nonrepeating loop, like onTriggerEnter.