Graphics Reference
In-Depth Information
A repeating call to the function doSpawn() will be made with Unity's InvokeRepeating()
function. InvokeRepeating will keep on calling doSpawn() until a CancelInvoke() tells it
to stop or the scene is removed from memory. Before scheduling a new repeat call, this
code uses CancelInvoke() to make sure that any previous call is well and truly stopped:
CancelInvoke("doSpawn");
InvokeRepeating("doSpawn",timeBetweenSpawns,timeBetweenSpa
wns);
}
doSpawn() is the function that is repeatedly called automatically by InvokeRepeating()
calls. It calls SpawnObject() to add the enemy to the scene:
void doSpawn()
{
SpawnObject();
}
SpawnObject() checks the spawn counter first to see whether the wave has finished or
not. When the variable spawnCounter is greater than totalAmountToSpawn, the repeat-
ing Invoke call needs to be cancelled by a CancelInvoke():
public void SpawnObject()
{
if(spawnCounter>=totalAmountToSpawn)
{
// tell your script that the wave is finished here
CancelInvoke("doSpawn");
When this wave has finished, this script disables itself and drops out to avoid using
any unnecessary CPU cycles:
this.enabled=false;
return;
}
The object (enemy) is created by the singleton-type instance of the SpawnController.
Its SpawnGO() creates the object and returns a reference to the new object's gameObject:
// create an object
tempObj=SpawnController.Instance.SpawnGO(spawnObjectPrefabs
[currentObjectNum],myTransform.position,Quaternion.identity);
// tell object to reverse its pathfinding, if required
tempObj.SendMessage('SetReversePath', shouldReversePath,
SendMessageOptions.DontRequireReceiver);
The newly spawned object needs to know which waypoint controller to use. Rather
than go through and try to find its control script, we use gameObject.SendMessage() to
send a message and a single parameter to the object. Any script component attached to the
gameObject that has a function named SetWayController() will be able to receive it. Just
in case there are no scripts with this function name in, a final (optional) parameter in the
Search WWH ::




Custom Search