Let's keep things simple on our first foray into exceptions and make sure that our level only restarts if we provide a positive scene index number:
- Open up Utilities and add in the following code to the overloaded version of RestartLevel():
public static class Utilities
{
public static int playerDeaths = 0;
public static string UpdateDeathCount(out int countReference)
{
// ... No changes needed ...
}
public static void RestartLevel()
{
// ... No changes needed ...
}
public static bool RestartLevel(int sceneIndex)
{
// 1
if(sceneIndex < 0)
{
// 2
throw new System.ArgumentException("Scene index cannot
be negative");
}
SceneManager.LoadScene(sceneIndex);
Time.timeScale = 1.0f;
return true;
}
}
- Change RestartLevel() in the OnGUI() method...