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.

  1. As shown in the following GameData screenshot, add lines 10 and 11:
    Time for action – setting the Lives for Player
  2. As shown in the following SetupState screenshot, insert the section of code in the red box:
    Time for action – setting the Lives for Player

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 left: displayed, along with the value retrieved from the playerLives variable in GameData
  • The Scripting Reference specifies the text shown in the GUI.Box needs to be of type String
  • The string.Format() method is C# code that makes everything in the parentheses a String
  • The manager.gameDataRef.playerLives statement is retrieving the value stored in playerLives before being converted to a String for display

Line 34: GUI.Box(new Rect(Screen.width -110,40,100,120), "Player Lives");

  • This displays a GUI Box as a background for three buttons
  • The title at the top is Player Lives

Line 36: if(GUI.Button(new Rect(Screen.width - 100,70,80,20), "5"))

manager.gameDataRef.playerLives = 5;

  • This if statement checks to see if a GUI Button is clicked
  • When clicked, the value 5 is assigned to the variable playerLives in GameData using Dot Syntax

Lines 39 and 42:

  • These are similar to line 36 except for the value assigned to playerLives

With all this coding complete, you should have Scene0 looking like this when in SetupState:

What just happened?

If you are using Cube instead, perhaps you have something like this:

What just happened?

Have a go hero – changing the setup spin speed

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.

Pop quiz – understanding GameObjects

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?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.139.224