How would I make an object destroy itself, and then spawn a new instance of itself

Hi guys , how How would I make an object destroy itself, and then spawn a new instance of itself, but with the new instance like +(10,0,0) forward from where the previous object was .

So far I do have the destroy itself script down, with a slight delay(tillgone)

var tillgone : float =1 ;


Destroy (gameObject, tillgone);

I just need to add something like

{ clone gameObject ( (transformposition + ( 10,0,0)}

that i’m guessing needs to execute before this game object is destroyed . Should I do this as a separate script and have it order first in the script execution manager ?

If I got it right, you want do something like this:

  1. Duplicate current game object to another position
  2. Destroy current gameobject

Right? o.O

Well, I think it can be done with this:

var difference : Vector3 = Vector3(10, 0, 0);

function Start () {
	Instantiate(gameObject, transform.position + difference, transform.rotation);
	Destroy(gameObject);
}

Yes, for some reason unity is kicking back an error saying the script hasn’t been complied yet when I try to apply it an object , stay tuned though …

I have no idea what’s wrong with it :S I tried again and it works with me without problems o.O

Never mind, it works now , any idea how i would add a
yield wait time in their …
That would make it perfect

Where do you want that yield? After destroy (destroy → wait → duplicate) or after duplicate (duplicate → wait → destroy)?

yield WaitForSeconds (x);

“x” is whatever time you want to wait for (2, 6, 13, etc).

http://unity3d.com/support/documentation/ScriptReference/WaitForSeconds.html

The delay is before the duplicate
Like the object hangs out a bit at (10,0,0) then destroys and respawns elsewhere

Where would i place a yield time statement, it wont complie as is

var difference : Vector3 = Vector3(10, 0, 0);

 

function Start () 
{
yield WaitForSeconds (3);
{

    Instantiate(gameObject, transform.position + difference, transform.rotation);

    Destroy(gameObject);

}
}

Start needs to become IENumerator
Atleast it does in C#

function Duplicate (waitTime : float, newPosition : Vector3) {
	yield WaitForSeconds(waitTime);
	Instantiate(gameObject, transform.position + newPosition, transform.rotation);
	Destroy(gameObject);
}

So you would call Duplicate like this:

function Start () {
	Duplicate(1.5, Vector3(0, 1, 0));
}

That would do this

Wait 1.5 seconds
Duplicate first object to +1 point upwards
Destroy first object

Wait 1.5 seconds
Duplicate second to +1 point upwards
Destroy second object

Thanks so much
I ended up modifying it a bit though, since for some objects I needed to have it respawn at a specific point

Very useful for a game when you need an action to effectively loop

var posX : float = 3;
var posY : float = 3;
var posZ : float = 3;
var tillgone : float = 3;
function Start () {

    Duplicate(tillgone, Vector3(posX, posY, posZ));

}



function Duplicate (waitTime : float, newPosition : Vector3) {

    yield WaitForSeconds(waitTime);

    Instantiate(gameObject, Vector3(posX, posY, posZ), transform.rotation);

    Destroy(gameObject);

}