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;
}
}