Game Development Reference
In-Depth Information
Player Settings
There are a few remaining issues that could be improved, but now that you can go back and forth
from the game and menus, you can see about implementing the player options. To begin, you will
send the updated values to the LevelManager before returning to the main menu.
1.
Open the SettingsGUI script.
2.
Add the following variable:
LevelManager levelManager; // the script that holds the data between levels
Add the usual code to identify the LevelManager in the
Start
function:
3.
if(GameObject.Find("Level Manager")) {
levelManager = GameObject.Find("Level Manager").GetComponent<LevelManager>();
}
In the
OnGUI
function, inside the if
(GUI.changed)
clause, add
4.
// Send updated values back to LevelManager
if (levelManager) {
levelManager.ambientVolume = ambSliderValue;
levelManager.difficulty = (int)(diffSliderValue);
}
Once again, you may wish to run the game directly from the garden level during testing, so you are
checking for the existence of the LevelManager before sending it the updated information. This will
prevent communication errors when there is no LevelManager present, but any functionality related
to the Level Manager will not be accessible.
The sliders use float values, but the difficulty functionality will be easier to manage as a rounded-off
integer, so the float is
cast
or converted to an integer where it becomes a number from 0 to 10.
Because the number will be used mainly to adjust timing, a high number will give the player longer to
get things done and a lower number will cause things to happen more frequently, so the difficulty will
range from 10 down to 0.
5.
Save the script.
Next, you will add the variables that will store the ambient sound volume and the game difficulty.
6.
In the LevelManager script, add the following variables:
internal float ambientVolume = 0.8f; // volume for all ambient sounds/music
internal int difficulty = 5; // affects battery life
7.
Save the script, click Play from the StartMenu level, and go to the
Settings menu.