I cobbled this script together in an attempt to fade out an object after its hitpoints are < 0, followed by a destroy object command. I don’t understand why it is crashing Unity every time I run it. Does anyone have a solution?
Thanks,
Greg
var hitPoints = 100.0;
var deadReplacement : Transform;
var dieSound : AudioClip;
var initialDelay = 1.0;
var fadeSpeed = 1.0;
function ApplyDamage (damage : float) {
// We already have less than 0 hitpoints, maybe we got killed already?
if (hitPoints <= 0.0)
return;
hitPoints -= damage;
if (hitPoints <= 0.0)
{
renderer.material.color.a = 1.0;
yield new WaitForSeconds(initialDelay);
while (renderer.material.color.a > 0.0)
color = renderer.material.color;
color.a = 0.0;
renderer.material.color = Color.Lerp(renderer.material.color, color, fadeSpeed * Time.deltaTime);
yield;
Detonate();
}
}
function Detonate () {
// Destroy ourselves
Destroy(gameObject);
// Play a dying audio clip
if (dieSound)
AudioSource.PlayClipAtPoint(dieSound, transform.position);
// Replace ourselves with the dead body
if (deadReplacement) {
var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);
}
}