x


Destroy an object created with Instantiate()

Hi :) So I have an object. When it is created, it also creates a second one in the same position (which are particles). When the player hits the object, this object is destroyed. That's the script here, which is working.

Except that I can't destroy the particle object created in the function Start....

var prefab : Transform;

function Start () {
   var clone  = Instantiate (prefab, Vector3(transform.position.x,
   transform.position.y,
   transform.position.z),
   Quaternion.identity);
}


function Update () {
   transform.Rotate(Vector3.up * Time.deltaTime*50);
   transform.Rotate(Vector3.right * Time.deltaTime*50);
}


function OnTriggerEnter(collisionInfo : Collider){
      Destroy (clone); // <======= THE ERROR IS THIS LINE
      Destroy (gameObject);
      //  Destroy(collisionInfo.gameObject);
}

So my error is the line with :

Destroy (clone);

If anyone could help me, that would be very nice....

more ▼

asked Mar 24 '10 at 07:03 PM

CyberTwister gravatar image

CyberTwister
16 1 1 2

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

5 answers: sort voted first

The problem is that the "clone" variable is declared in Start, so it only exists in Start, and not in OnTriggerEnter. You have to make "clone" be a global variable instead of a local variable.

more ▼

answered Mar 24 '10 at 07:15 PM

Eric5h5 gravatar image

Eric5h5
80k 41 132 518

var prefab : GameObject; var clone : GameObject;

function OnTriggerEnter(collisionInfo : Collider){ Destroy (clone); Destroy (gameObject); }

Does'nt work too .... :(

Mar 24 '10 at 07:31 PM CyberTwister
(comments are locked)
10|3000 characters needed characters left

You're instantiating a prefab called "Clone" then later trying to Destroy "Clone". Problem is I THINK that what you need to do is to destroy the Clone(Clone) (as appears in Inspector when you play)

Make sure the prefab has a tag - e.g. name the tag "Clone". Then have:

Destroy(GameObject.FindWithTag("Clone")};

Get rid of the Destroy (clone); Destroy (gameObject) stuff you have etc and just have the above - see if that works??

more ▼

answered May 18 '10 at 11:46 AM

Bob 3 gravatar image

Bob 3
42

Bad idea - there could be many objects with that tag. The OP is close, just needs to follow the other Answers and make the variable non-local.

Jun 21 '10 at 06:11 PM Cyclops
(comments are locked)
10|3000 characters needed characters left

Your problem is called variable scoping (what Eric5h5 said, and I voted up his answer for that). To clarify:

var prefab : Transform;  // this var is scoped to entire class

function Start () {

   // this var is scoped to ONLY the start function

   var clone  = Instantiate (prefab, Vector3(transform.position.x,
   transform.position.y,
   transform.position.z),
   Quaternion.identity);

}

So all you need to do is this:

var prefab : Transform;
var clone : GameObject; // now available for all functions

function Start () {
   clone  = Instantiate (prefab, Vector3(transform.position.x,
   transform.position.y,
   transform.position.z),
   Quaternion.identity);
}
more ▼

answered Jun 21 '10 at 05:51 PM

BoredKoi gravatar image

BoredKoi
391 7

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

Eric5h5 is correct.

You are declaring the variable twice by saying var clone= both as a statement and in the function. In the function just say clone = not var clone = .

more ▼

answered Nov 26 '10 at 03:21 PM

Mr_T gravatar image

Mr_T
4 3 3 3

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

I'm late but....

I've found a way to Do this and works for me.

I attach one Script in the prefab so when i Instanced this in other Variable....

i call to their self destruction function. obviously declare the variable global

/////////////////////

var instant = null; var pref : GameObject; //load in inspector , it has the script with the destroy function

function Start(){ { instant=Instantiate(pref,Vector3(0,2,0), Quaternion.identity); instant.selfDestroy();//in this example is created and destroy inmediatly }

function myDestroy(){//this is call when i need destroy instant { instant.selfDestroy(); }

so.....

//in instant attached Script

function selfDestroy(){ Destroy(gameObject);//SelfDestroy

}

//and Destroy only the Instant clone

Bye !! and luck

more ▼

answered Feb 03 '12 at 04:10 AM

IvanLeon gravatar image

IvanLeon
1

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

x1667
x764

asked: Mar 24 '10 at 07:03 PM

Seen: 15044 times

Last Updated: Feb 03 '12 at 04:10 AM