Destroying and Respawning

I've been trying a host of things to get this respawner to work, but either the script doesn't heed the new "debriCreate" function call after the initial spawn is destroyed as in the following case, or it tells me that the clone has already been destroyed and won't respond.

Any ideas?

var debri : GameObject;
var startSpot : Transform;
var endSpot : Transform;

private var currentDebri : GameObject;

function Start () {
    debriCreate ();
}

function Update () {
    if (currentDebri == false)
    	debriCreate ();
    debriMove ();
    debriDestroy ();
}

function debriMove () {
    currentDebri.transform.position = Vector3.Lerp (startSpot.transform.position, endSpot.transform.position, Time.time);
    currentDebri.transform.Rotate (Vector3(15, 3, 22) * Time.deltaTime);
}

function debriDestroy ()
    if (currentDebri.transform.position == endSpot.position) {
    	Destroy (currentDebri);
}

function debriCreate () { currentDebri = Instantiate (debri, startSpot.transform.position, startSpot.transform.rotation); }

In addition to the code suggestions, this logic looks like you will potentially be creating and destroying a lot of objects, which is not recommended for performance reasons, particularly since it will unnecessarily clog the garbage collector.

Instead of destroying the debris, consider disabling the gameObject, reverting its state back to the original state (moving it back to the start, resetting any parameters), and then enabling it again.

This will not only save on performance, but also prevent GC related issues in the future.

Also, why not simply create the debris at the end of the destroy function, rather than continually poll to see if the debris exists? Unless there is some way the debris can be 'destroyed' without your destroy function being invoked, that polling should be unnecessary.

Hey I was able to get your script working (on a Netbook in Jury Duty no less! Thanks for help passing the time)...

I think you have two problems, really. I'm, not a "real programmer" so there may be better ways to fix this but here's what I suggest to get it working

First thing is that you are checking for the positions to be exactly equal, and that never really happens due to the precision (or lack of). I could be wrong but I don't even think it is comparing the x/y/z anyways, its more comparying if the transforms are the same reference, which they aren't so it's never true.

Instead of checking for:

 if (currentDebri.transform.position == endSpot.position)

I did:

  if (Vector3.Distance(currentDebri.transform.position, endSpot.position) < 0.2)

So I am checking if the distance is less than a certain amount, then destroy it.

Secondly, the way you are using Lerp doesn't seem to work out. You are telling it to animate from point A to B over time.time, but time.time keeps increasing, and the lerp function wants a number 0 to 1. So What I did was subtract the time the object was created, so the number starts at 0... So try this:

At the top declare:

var debriTime;

Then, in your createDebri function add:

debriTime = Time.time; // time the debri was created

Finally update your Lerp line to look like:

currentDebri.transform.position = Vector3.Lerp (startSpot.transform.position, endSpot.transform.position, (Time.time - debriTime));

And bam I had debris flying and destroying across the screen non-stop.

Hope that helps. Hopefully I didn't miss anything on this cramped screen.

And looks like my Jury Duty is over! I can go home! Yay!