Let's make our code a little more robust so that it can cope with the issue of an empty spawnPoints array; that is, when no objects are tagged as Respawn in the scene.
To cope with no objects being tagged as Respawn, we need to do the following:
- Improve our Start() method in the C# script class called SpawnPointManager so that an error is logged if the array of the objects tagged as Respawn is empty:
void Start() {
spawnPoints = GameObject.FindGameObjectsWithTag("Respawn");
// logError if array empty
if(spawnPoints.Length < 1)
Debug.LogError ("SpawnPointManagaer - cannot find any objects
tagged 'Respawn'!");
}
- Improve the RandomSpawnPoint() and NearestSpawnpoint() methods in the C# script class called SpawnPointManager so that they still return a value (that is, null), even if the array is empty:
public GameObject RandomSpawnPoint ...