Game Development Reference
In-Depth Information
if(Input.GetKey(KeyCode.C)) {
sphereCollider.enabled = !sphereCollider.enabled;
}
}
function NewColor() {
red = RandomColorValue();
green = RandomColorValue();
blue = RandomColorValue();
alpha = RandomColorValue();
gameObject.renderer.material.color = Color(red, green, blue, alpha);
waitTime = 5;
}
function OnMouseDown() {
NewColor();
}
Save the script and give it a try.
Destroying Game Objects
A more permanent option than enable/disable is using the Destroy() function to remove
components or entire game objects. This can be accomplished a number of different ways.
One way might be with a timer similar to our countdown to turning the sphere blue. Using the double
forward slash, comment out the line of code in the Update() function that changes the sphere color
to blue in order to deactivate it.
//gameObject.renderer.material.color = Color.blue;
Immediately underneath it, and still within the curly braces of the conditional statement add:
Destroy(gameObject);
Save and play. Instead of turning blue after five seconds, the sphere disappears.
Back in the script, comment out the call to Destroy() . With both lines commented out, nothing will
change at the five-second mark. Using the double forward slash to comment out code is a great
shortcut to enable and disable short blocks of code for testing or debugging instead of having to
retype or cut and paste lines of code.
You can let the player destroy the sphere by adding another GetKey() conditional to the Update()
function as follows:
if(Input.GetKey(KeyCode.D)) {
Destroy(gameObject);
}
 
Search WWH ::




Custom Search