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!