Trying to respawn prefabs after destroying them with a reset button

Hello All, I am a scripting newbie and I’m running into some serious problems. What I am doing is that at the start of the scene/level a grid of 9 prefab(Cubes) Instantiates. Also on the screen I have a “Reset” GUI button that is supposed to destroy all the prefabs(Cubes) and then respawn them. There needs to be a slight time delay between the time the cubes are destroyed from clicking the reset button to when the cubes respawn, so it the user can visually see it. I can’t use Application.Loadlevel on the Reset button either, as the parameters of this project forbid me from doing such. I tried calling the function used to spawn the cubes in the script for the reset button, either its not working or since there is no time delay I can’t see it happening. The following is my code:

Cube Spawning Script:

//the WoodCube Prefab assigned in the inspector
public var thePrefab:GameObject;

//the # of WoodCubes that will be spawned 
public var numberOfCubes:int = 9;

//the ARRAY of spawnpoints that our cube will be spawned at
private var spawnPointList:GameObject[];

//array of which spawn points are currently available for spawning at
private var spawnIndexAvailableList:Array = [];

//variable to hold the total number of spawn points, saves having to recalculate
private var numberOfSpawnPoints:int;

function Awake()
{
	//retrieve GameObject tagged as "SpawnPoint" within the "CubeSpawnPoints" GameObject which the script is a component of
	spawnPointList = gameObject.FindGameObjectsWithTag("SpawnPoint");
	
	//retreive # of spawn points
	numberOfSpawnPoints = spawnPointList.length;
	
	//make sure number of cubes doesn't exceed number of spawn points
	if (numberOfCubes > numberOfSpawnPoints) numberOfCubes = numberOfSpawnPoints;
	
	//make all spawn points available by setting each index to true
	for (var i:int = 0; i < numberOfSpawnPoints; i++)
	{
		spawnIndexAvailableList *= true;*
  • }*

  • //spawn X amount of cubes according to numberOfCubes*

  • for (var j:int = 0; j < numberOfCubes; j++ )*

  • {*

  •  SpawnCube();*
    
  • }*
    }

function SpawnCube()

  • {*

  •  //generate a random integer to use as the index to select a spawn point from the list*
    
  •  var randomSpawnIndex:int = Random.Range(0, numberOfSpawnPoints);*
    
  •  //while the selected spawn index is unavaiable regenerate another one*
    
  •  while (!spawnIndexAvailableList[randomSpawnIndex])*
    
  •  {*
    
  •  	randomSpawnIndex = Random.Range(0, numberOfSpawnPoints);*
    
  •  }*
    
  •  //retrieve the pos and rot of the cube's spawn point*
    
  •  var spawnedCubePosition:Vector3 = spawnPointList[randomSpawnIndex].transform.position;*
    
  •  var spawnedCubeRotation:Quaternion = spawnPointList[randomSpawnIndex].transform.rotation;*
    
  •  //instantiate (create) the WoodCube prefab with the above position and rotation*
    
  •  var spawnedCube:GameObject = Instantiate(thePrefab, spawnedCubePosition, spawnedCubeRotation);*
    
  •  //set the spawned cube as a child of the "CubeSpawnPoints" gameObject that this a Component of*
    
  •  //this is so we can use SendMessageUpward within scripts attached to the WoodCube prefab to call functions within this script*
    
  •  spawnedCube.transform.parent = spawnPointList[randomSpawnIndex].transform;*
    
  •  //set the name of the cube as its index*
    
  •  spawnedCube.name = randomSpawnIndex.ToString();*
    
  •  //make the spawn index unavailable to prevent another cube being spawned in this position*
    
  •  spawnIndexAvailableList[randomSpawnIndex] = false;*
    
  • }*

Here is the Reset button script:
//Includes both Back and Reset GUI Buttons
var spawnpoint:GameObject;

function OnGUI()

  • {*

  •  //Back Button takes you back to Main Menu*
    
  •  if (GUI.Button(Rect(50,50,200,100),"Back"))*
    
  •  	Application.LoadLevel("MainMenu");*
    
  •  	//Reset Button destroys cube grid and reloads	*
    
  •  if (GUI.Button(Rect(1430,50,200,100),"Reset"))*
    
  •  	Destroy(spawnpoint);*
    
  •  GUI.color = Color.green;*
    
  •  GUI.Button(Rect(50,50,200,100),"Back");*
    
  •  GUI.Button(Rect(1430,50,200,100),"Reset");*
    
  • }*

  • var CubeController : CubeController;*

function Update()

  • {*

  •  CubeController.Awake();*
    
  • }*
    Obviously I’m writing in JavaScript as I’m not well versed in C# just yet. I’m really new at coding and pick things up fairly quickly, but I’m having a hard time doing this. Any help would be greatly appreciated. Thank you Community!

Alright, here’s your CubeController:

#pragma strict

	var theCube:GameObject; //Cube prefab
	var cubesContainer:GameObject; //Cube container prefab
	var spawnedCube:GameObject; //Individual spawned cube instantiated from theCube
	var allCubes:GameObject; //Cube container instantiated from cubesContainer
	var spawnPointPositionsList : Vector3[]; //Array of positions for cubes
	var spawnPointRotationsList : Quaternion[]; //Array of rotations for cubes
	var spawnIndexAvailableList : boolean[]; //Array of booleans to check for available spawn points
	var numberOfCubes:int = 9;
	var randomSpawnIndex:int;

	function Start () {
		spawnPointPositionsList = new Vector3[numberOfCubes]; //Initialize array of cube positions
		spawnPointRotationsList = new Quaternion[numberOfCubes]; //Initialize array of cube rotations
		spawnIndexAvailableList = new boolean[numberOfCubes]; //Initialize array of booleans
		PopulateSpawnPointList (); //Assign spawn point values to arrays
		SpawnAllCubes (); //Spawns initial set of cubes
	}

	function SpawnAllCubes(){

		allCubes = Instantiate (cubesContainer, cubesContainer.transform.position, cubesContainer.transform.rotation) as GameObject;
        allCubes.name = "allCubes";
		for (var i = 0; i < numberOfCubes; i++)
		{
			spawnIndexAvailableList *= true;*
  •  }*
    
  •  for (var j:int = 0; j < numberOfCubes; j++ )*
    
  •  {*
    
  •  	SpawnCube();*
    
  •  }*
    
  • }*

  • function SpawnCube(){*

  •  randomSpawnIndex = Random.Range(0, numberOfCubes);*
    
  •  while (!spawnIndexAvailableList[randomSpawnIndex])*
    
  •  {*
    
  •  	randomSpawnIndex = Random.Range(0, numberOfCubes);*
    
  •  }*
    
  •  spawnedCube = Instantiate (theCube, spawnPointPositionsList[randomSpawnIndex], spawnPointRotationsList[randomSpawnIndex]) as GameObject;*
    
  •  spawnedCube.transform.parent = null;*
    
  •  spawnedCube.transform.parent = allCubes.transform; //Make spawnedCube a child of allCubes, so destroying allCubes destroys all spawned cubes*
    
  •  spawnedCube.name = "cube" + randomSpawnIndex.ToString();*
    
  •  spawnIndexAvailableList[randomSpawnIndex] = false;*
    
  • }*

  • function PopulateSpawnPointList(){*

  •  for (var n = 0; n < numberOfCubes; n++) {*
    

_ var spawnPosX = n10;_
_ var spawnPosZ = n
10;_

  •  	spawnPointPositionsList[n] = new Vector3(spawnPosX, 0, spawnPosZ);*
    
  •  	var spawnRotX = 0;*
    
  •  	var spawnRotY = 0;*
    
  •  	var spawnRotZ = 0;*
    
  •  	spawnPointRotationsList[n] = Quaternion.Euler(spawnRotX, spawnRotY, spawnRotZ);*
    
  •  }*
    
  • }*
    And here’s your Reset:
    #pragma strict

function OnGUI(){

  • GUI.color = Color.green;*
  • if (GUI.Button(Rect(100,50,200,100),“Reset”))*
  • {*
  •  Destroy(GameObject.Find("allCubes"));*
    
  •  welcomeBackCubes();*
    

}
}

function welcomeBackCubes()
{

  • yield WaitForSeconds (2);*
  • GameObject.Find(“Main Camera”).SendMessage(“SpawnAllCubes”); //“Main Camera” is whatever GameObject the CubeController script is attached to*
    }