Objects will not destroy after they have been instantiated

So here is my code. I am not a programmer so I probably have done quite a few things wrong. I am an artist and this is a prototype for a project I hope to create. So I create these objects randomly (for demo purposes they are cubes and spheres) and after a certain time I want them to be destroyed and new objects replace them. However instead of the clones being destroyed the gameobject this script is attached to is destroyed.

var grid: int = 4; //size of grid

var randomnum : float = 0.1; //random number generator

var isSpawned : boolean = false;

var spawncubePrefab : GameObject; //cube

var spawnspherePrefab : GameObject; //sphere

function Start () {

}

function Update () {

if (isSpawned == true){

return;

}

else {

	CreateGrid();

	isSpawned = true;

	DestroyClones();

}

}

function CreateGrid() {

for (var i=1; i<(grid+1); i++){

	var zpos= (2.5*((i*i)+i+4)); //creates rows at certain distance

	for (var j=0; j randomnum) {	

			Instantiate(spawncubePrefab, Vector3(xpos,2,zpos), transform.rotation); 

			Debug.Log("I created a cube");

		}

		else {

			Instantiate(spawnspherePrefab, Vector3(xpos,2,zpos), transform.rotation);

			Debug.Log("I created a sphere");

		}

	}	

}

}

function DestroyClones() {

var randomtime = Random.Range(3,7);

print (randomtime);

yield WaitForSeconds (randomtime);

Destroy(gameObject);

isSpawned = false;

Debug.Log("Destroyed");

}

@jkish1990, this damned UA text editor seems to have eaten part of your code, in the j loop. I don’t know what exactly it was before, but tried to calculate xpos in some logical manner, and selected the prefab randomly based on a 50%/50% cube/sphere ratio.

Anyway, you were destroying gameObject, which’s the reference to the object to which this script is attached - you should instead destroy the objects you have created, but this is impossible at DestroyClones because you don’t have references to the objects! Fortunately, there’s a simple solution in this case: call Destroy(clone, randomtime) right after creating an object (where clone is a reference to the object created) - this will kill the object after a randomtime delay. Since all objects are created in the same frame, they will also die in a single frame, randomtime seconds after - just wait the randomtime delay and clear the variable isSpawned:

var grid: int = 4; //size of grid
var isSpawned : boolean = false;
var spawncubePrefab : GameObject; //cube
var spawnspherePrefab : GameObject; //sphere

function Update () {
  if (isSpawned == true){
    return;
  }
  else {
    CreateGrid();
    isSpawned = true;
  }
}

function CreateGrid() {
  var randomtime = Random.Range(3,7); // generate the random life time
  for (var i=1; i<(grid+1); i++){ // create the objects
    var zpos= (2.5*((i*i)+i+4)); //creates rows at certain distance
    for (var j=0; j < grid; j++){
      var xpos = 2.5 * j; //creates columns at certain distance
      var prefab: GameObject;
      if (Random.value >= 0.5) { // select the prefab randomly
        prefab = spawncubePrefab;
      } else {
        prefab = spawnspherePrefab;
      }
      // create the object and get a reference to it:
      var clone: GameObject = Instantiate(prefab, Vector3(xpos,2,zpos), transform.rotation);
      // program the object destruction time:
      Destroy(clone, randomtime);
    }
  }
  yield WaitForSeconds(randomtime); // wait randomtime...
  isSpawned = false; // then clear isSpawned, since all objects have suicided by now
}