Instantiating and Destroying prefabs with JavaScript problem?

Hello, I’m kinda new to JavaScript coding with Unity. I’m trying to make a game, so I created a road and imported some animated cars. I tried writing a script to Instantiate a car, translate it for some time using the Time class and destroy it after some time, say 30 seconds.

#pragma strict
public var Speed : float = 50.0;
public var peugeot : Rigidbody;
public var nextSpawn : float = 0.0;
public var spawnRate : float = 5;
public var endTime : float = 20;
public var MoveDirection : Vector3 = Vector3.zero;

function Movement () {
	MoveDirection = Vector3(0,0,-1);
	this.transform.Translate((MoveDirection.normalized * Speed) * Time.deltaTime);
}

function Update () {
if (Time.time > nextSpawn) {
	nextSpawn = Time.time + spawnRate;
	var peugeotClone = Instantiate(peugeot, transform.position, transform.rotation);

	Movement();

}
if (Time.time == endTime)
	Destroy(peugeotClone, 20);
}

I want the script to repetitively Instantiate this particular prefab car, translate it for some time and destroy it. That’s all I want, but I don’t seem to be getting it. Whenever I assign the script I wrote to the car, it starts creating clones of clones, more like a chain reaction, ends up eating all my memory and Unity hangs and I have to restart. Please can any one help me? A detailed explanation and script will be very much appreciated. Thank you :slight_smile:

Is this script actually attached to the car-prefab? If so it’s clear that you would get infinte clones,
because everytime you create a car with the script attached to it, it starts the same process of instantiating over again.
A better way would be to create for example an empty object with a script for spawning logic. The functions for moving the car around would of course stay on the object itself.
If there is not much complexitiy the easiest way of destroying the car after some time would be to go with yield WaitForSeconds in the Start-Function of der Car-Script.

Hope that helps

Okay, I think I get you, but I don’t understand how to implement the WaitForSeconds. Please could you give a small sample or a pseudocode or something? I’m really new to programming. Thank you.

Yield basically ‘stops’ the code below from being executed before time runs up.
For example:

function Start() {
      yield WaitForSeconds (20); //Will prevent the code below from bein executed before 20 run up
      Destroy(gameObject);       // After 20 seconds the gameObject will be destroyed
      Destroy(this);            // Right after that, the script attached will be destroyed
}

Also you should use only in functions like Start() and not in for example Update().

Thank you so much, I got it figured out. I have a new challenge. Can I use this same technique to repetitively create and destroy environmental objects relative to the main character? I’m trying to create something like an endless runner, where the character chases another character infinitely. I don’t know the best way to tackle this. Should I make the environment create, translate and destroy while the character is static, or the character moves and the environment just creates and destroy?

Is instantiating and destroying this way really expensive to the processor, cos the game is being developed for Android? What other methods would you recommend to do this?

Thank you.