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.
GameData
screenshot, add lines 10 and 11:SetupState
screenshot, insert the section of code in the red box:Let us analyze the code shown in the preceding screenshots:
In GameData
:
Line 11: public int playerLives;
playerLives
holds the remaining Lives availableSetupState
Line 10: [HideInInspector]
playerLives
should not be editable in the InspectorIn SetupState
:
Line 31: GUI.Box(new Rect(Screen.width - 110,10,100,25), string.Format("Lives left: "+ manager.gameDataRef.playerLives));
playerLives
variable in GameData
GUI.Box
needs to be of type String
string.Format()
method is C# code that makes everything in the parentheses a String
manager.gameDataRef.playerLives
statement is retrieving the value stored in playerLives
before being converted to a String
for displayLine 34: GUI.Box(new Rect(Screen.width -110,40,100,120), "Player Lives");
Line 36: if(GUI.Button(new Rect(Screen.width - 100,70,80,20), "5"))
manager.gameDataRef.playerLives = 5;
if
statement checks to see if a GUI Button is clicked5
is assigned to the variable playerLives
in GameData
using Dot SyntaxLines 39 and 42:
playerLives
With all this coding complete, you should have Scene0 looking like this when in SetupState
:
If you are using Cube instead, perhaps you have something like this:
The initial Player spin speed is what I happen to like. Change it to your liking in the Inspector, and then modify the variable in code so this spin speed will always be the default setting. Remember, changing the setting in the Inspector does not change any code files.
Q1. For writing scripts, what are the two primary Unity documents you should use?
Q2. What normally happens to existing GameObjects when a new Scene is loaded?
Q3. The States require access to data stored on GameData
. Why do the States use StateManager
to access this data instead of directly accessing GameData
?
Q4. When Player is added to the Scene or Hierarchy, why is it made a child GameObject of the GameManager GameObject?
3.21.46.78