Game Development Reference
In-Depth Information
You have now reached the “point of no return” in your game, or more accurately, the “point of must
return.” Because the Level Manager is only present in the StartMenu scene and is carried forward
with each loaded level, you must always play from the StartMenu scene to check the “scene
hopping” functionality.
1.
Open the StartMenu scene.
2.
Click Play, and test the gnome's repositioning by accessing the main menu
from inside the garden and then returning to the game.
All works reasonably well when you leave the active game for the MainMenu and then return to the
game. The Gnomatic Garden Defender is put into its former location and orientation. Conspicuously
missing, however, are the zombie bunnies and the HUD.
Let's tackle The HUD next. There are only two pieces of data that must be saved: the zombie-bunny
count and the battery life remaining. Both must be saved in their current value just before the level
change.
1.
In the LevelManager script, add the new storage variables:
internal int bunCount = 0; // number of buns in garden
internal float batteryRemaining; // time in seconds
2.
Save the script.
3.
In the GameMisc script, add the following variables:
// needed for level hopping
public ScoreKeeper scoreKeeper; // where the current bun count is kept
(on the Game Manager)
public BatteryHealth batteryHealth; // where the battery charge is tracked
(on the Battery Life)
In the LevelPrep function, inside the if(LevelManager) clause, add the
following to inform the LevelManager of the current values:
4.
// send the current zombie bunnie count
levelManager.bunCount = scoreKeeper.currentBunCount;
// send the battery info
levelManager.batteryRemaining = batteryHealth.batteryRemaining;
5.
Save the script.
The console reports the two variables are inaccessible due to their protection level. The GameMisc
script, acting as a “middle man” doesn't have access to the variables on either end unless they are
made public or internal. You set the LevelManager's new variables as internal, but you will have to
locate and change the other two on their respective scripts.
In the ScoreKeeper script, set the CurrentBunCount variable to internal.
6.
7.
In the BatteryHealth script, set the batteryRemaining variable to internal.
8.
Save both scripts
 
Search WWH ::




Custom Search