Game Development Reference
In-Depth Information
Figure 4-11. Creating the SphereColor script
Open the script in MonoDevelop using the Open button in the Inspector Panel and you will find it already
has empty Start() and Update() functions. Add the following line of code to the Start() function:
gameObject.renderer.material.color = Color.red;
Save the script. In the editor, drag the script from the Project panel and drop it onto the Sphere in
either the Hierarchy view or Scene view to attach it to the Sphere game object. Play to see the red
sphere. The Start() function is called once, and the sphere material is set to red. Exit Play mode.
Breaking this line of code down shows the following:
(1) (2) (3) (4) (5) (6)
gameObject.renderer.material.color = Color.red;
1. gameObject refers to the sphere, as this is the game object to which
script is attached.
Using dot syntax, you drill down to
2.
the Mesh Renderer component,
3.
the attached default material, and
4.
the color property of the material.
Then you assign a value to the color property using
5.
the Color class,
which has a few shortcuts including
6.
red.
Color
You can +' on Color to see more about the class in the Scripting Reference, including other
shortcut colors available to you. Colors are made up of four properties: red, green, blue, and alpha.
Each of the three color properties is a float value between 0 and 1, where 0 is no color and 1 is the
full color. Colors are additive, so r, g, b values of 0, 0, 0 yield black, while 1, 1, 1 yields white.
 
Search WWH ::




Custom Search