Game Development Reference
In-Depth Information
Using the ! operator to flip a boolean value is often referred to as toggling because it behaves like a
physical toggle-switch that can only be flipped two ways, like a light switch between “on” and “off.”
In this example, the player pressing the C key works exactly like you checking or unchecking the box
to enable and disable the component in the Inspector. In fact, pay attention to the checkbox in the
Inspector panel during gameplay and you will see the box check and uncheck in response to your
pressing the C button.
So far, the complete listing of the script is as follows:
#pragma strict
// Declare the variables to contain the color values.
private var red : float;
private var green : float;
private var blue : float;
private var alpha : float;
public var waitTime : float = 5f;
private var sphereCollider : Collider;
function Start()
{
// On Start, set a random color for the sphere material.
NewColor();
// Print the random color values to the Console.
print(gameObject.renderer.material.color);
// Assign the sphere's collider component to the sphereCollider variable.
sphereCollider = GetComponent(Collider);
}
// Generate a random value between 0.000 and 1.000.
function RandomColorValue() {
var randomValue : float;
randomValue = Random.Range(0.000, 1.000);
return randomValue;
}
function Update()
{
waitTime -= Time.deltaTime;
if (waitTime < 0.0f) {
gameObject.renderer.material.color = Color.blue;
}
if(Input.GetKey(KeyCode.N)) {
NewColor();
}
 
Search WWH ::




Custom Search