Time for action – setting the Lives for Player
The Lives remaining for Player will be stored in a variable in GameData
. We don't want it in PlayerControl
because this information has no bearing on controlling Player.
- As shown in the following
GameData
screenshot, add lines 10 and 11: - As shown in the following
SetupState
screenshot, insert the section of code in the red box:
What just happened?
Let us analyze the code shown in the preceding screenshots:
In GameData
:
Line 11: public int playerLives;
- The variable
playerLives
holds the remaining Lives available - This value is set using GUI while in
SetupState
- As Lives are lost, this number will decrease toward zero
Line 10: [HideInInspector]
- The value in
playerLives
should not be editable in the Inspector
In SetupState
:
Line 31: GUI.Box(new Rect(Screen.width - 110,10,100,25), string.Format("Lives left: "+ manager.gameDataRef.playerLives));
- Lines 31 and 32 are only one line of code
- A GUI Box is shown on screen with the text Lives...