Game Development Reference
In-Depth Information
Although you can still playtest with warnings present, you definitely want to resolve these prior to
publication of your game.
Runtime Errors
Runtime errors are errors that occur after your scripts have been successfully compiled and your
game is running. The simplest example of a runtime error is a divide-by-zero error.
With the ImpactForce script still open in MonoDevelop, first delete or disable the warning in the
Start() function you created in the previous section. Now introduce a divide-by-zero error by editing
the ImpactForce() function to the following:
function ImpactForce () {
var colliders : Collider[] =
Physics.OverlapSphere(transform.position - Vector3(0, 4, 0), 10);
for(var cldr : Collider in colliders)
{
if(cldr.rigidbody == null) continue;
var a : int = 0;
cldr.rigidbody.AddExplosionForce(10, transform.position - Vector3(0,
4/a, 0), 10, 0, ForceMode.Impulse);
}
}
This breaks down as follows:
(1) var a : int = 0;
Declare an int type variable a and assign it the value of zero.
1.
(2) cldr.rigidbody.AddExplosionForce(10, transform.position - Vector3(0,
4/a, 0), 10, 0, ForceMode.Impulse);
Divide the y component of the Vector3 calculation by a to create the divide-
by-zero error.
2.
Save the script and playtest.
Notice that the script compiled, and no error messages appeared in the Console until after the scene
was running (Figure 10-20 ).
Figure 10-20. Runtime errors displayed in the Console
 
Search WWH ::




Custom Search