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.
Let us analyze the code shown in the previous two screenshots:
In PlayerControl
:
Line 8: public Color red = Color.red;
red
will store a value of type Color
red
variable is public
so that it will show in the Inspector to modify the colorred
variable needs to be accessed externally by SetupState
Lines 9 through 12:
In SetupState
:
Line 31: GUI.Box(new Rect(10,10,100,180), "Player Color");
Line 33: if(GUI.Button(new Rect(20,40,80,20), "Red"))
Line 34: controller.PickedColor(controller.red);
controller
is a reference to PlayerControl
—the Component attached to PlayerPickedColor()
method on PlayerControl
is calledcontroller.red
, is retrieving the value stored in the variable red
in PlayerControl
, which is the RGBA code for the color redPickedColor()
method and assigned to the parameter variable playerColor
In PlayerControl
:
Line 24: public void PickedColor (Color playerColor)
Color
, that will come from calling this method in SetupState
(see lines 34, 37, and so on, in SetupState
)public
because SetupState
needs to access itLine 26: renderer.material.color = playerColor;
renderer
stores a reference of the Mesh Renderer objectmaterial
stores a reference of the material applied to the meshcolor
stores the color applied to the materialplayerColor
variable stores the color received from SetupState
calling the PickedColor()
methodThe color of the mesh is now set until the game restarts.
In SetupState
:
Lines 36 through 46:
Line 48: GUI.Label(new Rect(Screen.width/2 -110, Screen.height - 100, 220, 40), "Hold Spacebar to pause rotation");
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))
18.188.227.4