Instantiate object problem.

Hello, I created an instantiate script and it looks like this:

var thePrefab : GameObject;
var timeDelay = 3;
function OnTriggerEnter (myTrigger : Collider){

if (myTrigger.gameObject.tag == "Player") {
while (true) {
        Instantiate( thePrefab, transform.position, Quaternion.identity );
        
yield WaitForSeconds(timeDelay);
    	transform.position.z+=0.4;
    	transform.position.x+=0.2;

  }
  }
  }

the problem is, when I leave the trigger box, it won’t delete the instantiated object. when I enter in the trigger box again, it will create another object.

HELP!!!

You should probably use this script instead.

var thePrefab : GameObject;
var timeDelay : float = 3 ;

function MoveInstances ( timeDelay2 : float )
{
   yield WaitForSeconds ( timeDelay2 ) ;
   transform.position.z+=0.4;
   transform.position.x+=0.2;
}

function OnTriggerStay ( ) // While is BADDDDDDD BADDDD BADDDD. Use this instead
{
   if ( myTrigger.gameObject.tag == "Player" )
   {
      Instantiate( thePrefab, transform.position, Quaternion.identity );
      MoveInstances ( timeDelay );

   }
}

And then on the Instantiated object, you need to have this script attached;

function OnTriggerExit ( )
{
   GameObject.Destroy ( gameObject ) ;
}

I hope this helps you . . . . Good luck, and let me know if this does not work for you, and we can find alternatives .