Time for action – changing the color using GUI buttons

Once again, we will add some code to PlayerControl and to the ShowIt() method of SetupState. We will be creating five GUI buttons to change colors. While we're at it, we'll also create a GUI label to tell the user to press the Space bar to pause Player rotation, and modify the button to switch States.

  1. The following screenshot is of the PlayerControl script. Add the code shown in the red boxes:
    Time for action – changing the color using GUI buttons
  2. The next screenshot is of the ShowIt() method of SetupState. Write your code as shown:
    Time for action – changing the color using GUI buttons

What just happened?

Let us analyze the code shown in the previous two screenshots:

In PlayerControl:

Line 8: public Color red = Color.red;

  • The variable red will store a value of type Color
  • The red variable is public so that it will show in the Inspector to modify the color
  • The red variable needs to be accessed externally by SetupState

Lines 9 through 12:

  • These are similar to line 8, just different colors

In SetupState:

Line 31: GUI.Box(new Rect(10,10,100,180), "Player Color");

  • This draws a rectangular box with a title of Player Color
  • This provides a background for the buttons
  • The example in the Scripting Reference is very similar

Line 33: if(GUI.Button(new Rect(20,40,80,20), "Red"))

  • The GUI.Button() method draws a button on the screen with a title of Red
  • This if statement detects when the button is clicked
  • When the button is clicked, GUI.Button() returns a value of true
  • Again, this is very similar to the example in the Scripting Reference

Line 34: controller.PickedColor(controller.red);

  • Using Dot Syntax, the value stored in the variable controller is a reference to PlayerControl—the Component attached to Player
  • The PickedColor() method on PlayerControl is called
  • The argument, controller.red, is retrieving the value stored in the variable red in PlayerControl, which is the RGBA code for the color red
  • This value is being passed to the PickedColor()method and assigned to the parameter variable playerColor

In PlayerControl:

Line 24: public void PickedColor (Color playerColor)

  • This is a method we create that will perform the color changing
  • It takes one parameter, Color, that will come from calling this method in SetupState (see lines 34, 37, and so on, in SetupState)
  • This method is public because SetupState needs to access it

Line 26: renderer.material.color = playerColor;

  • Every mesh that will be visible will have a Mesh Renderer Component object
  • The variable renderer stores a reference of the Mesh Renderer object
  • The variable material stores a reference of the material applied to the mesh
  • The variable color stores the color applied to the material
  • The playerColor variable stores the color received from SetupState calling the PickedColor() method

The color of the mesh is now set until the game restarts.

In SetupState:

Lines 36 through 46:

  • These behave exactly the same as line 33, except the colors are different

Line 48: GUI.Label(new Rect(Screen.width/2 -110, Screen.height - 100, 220, 40), "Hold Spacebar to pause rotation");

  • Lines 48 and 49 are a single statement. Since it's so long, it was put on two lines.
  • This is just a label displayed below the spinning Player to let the user know to press the Space bar to pause the spinning.

Line 51: if (GUI.Button(new Rect(Screen.width/2 -100, Screen.height - 50, 200, 40), "Click Here or Press 'P' to Play ") || Input.GetKeyUp(KeyCode.P))

  • Lines 51 and 52 are a single if statement
  • If the GUI button is clicked or the P key is pressed, line 54 is executed to switch States

Adding the Lives option for Player

The game will be lost by the Player losing all its Lives. The game will start with Player having 5, 10, or a 1000 Lives. The 1000 number is just a high number so that essentially the game isn't lost, at least for a very long time. That's why I called the button Can't Lose.

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

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