x


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;

    }

}

more ▼

asked Mar 17 '12 at 02:26 PM

jamessimo gravatar image

jamessimo
1 1 1 2

FIXED! The passenger var needed to be static.

Mar 17 '12 at 03:58 PM jamessimo
(comments are locked)
10|3000 characters needed characters left

2 answers: sort newest

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.

more ▼

answered Mar 17 '12 at 02:32 PM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

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

I think you need to call Destroy(gameObject); inside a nonrepeating loop, like onTriggerEnter.

more ▼

answered Mar 17 '12 at 02:29 PM

CC Inc gravatar image

CC Inc
206 8 13 18

(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:

x2090
x764
x573
x49

asked: Mar 17 '12 at 02:26 PM

Seen: 707 times

Last Updated: Mar 17 '12 at 03:58 PM