Why does this script suddenly stop before the end?

The following code is supposed to do three things:

  1. Apply Hitpoint damage 2) Fade up material 3) Instantiate a new object with audio.

Can someone explain to me why the script stops before it instantiates the new object at the end?

Thanks.

var hitPoints = 100.0;
var deadReplacement : Rigidbody;
var dieSound : AudioClip;
var explosion : Transform;
var damagesound : AudioClip;
var initialDelay = 1.0;
var fadeSpeed = 1.0;


var explosionRadius = 5.0;
var explosionPower = 10.0;
var explosionDamage = 100.0;

var explosionTime = 1.0;

function Start () {
	
	var explosionPosition = transform.position;
	var colliders : Collider[] = Physics.OverlapSphere (explosionPosition, explosionRadius);
	
	for (var hit in colliders) {
		if (!hit)
			continue;
		
		if (hit.rigidbody) {
			hit.rigidbody.AddExplosionForce(explosionPower, explosionPosition, explosionRadius, 3.0);
						
			var closestPoint = hit.rigidbody.ClosestPointOnBounds(explosionPosition);
			var distance = Vector3.Distance(closestPoint, explosionPosition);

			// The hit points we apply fall decrease with distance from the hit point
    	    var hitPoints = 1.0 - Mathf.Clamp01(distance / explosionRadius);
			hitPoints *= explosionDamage;

			// Tell the rigidbody or any other script attached to the hit object 
			// how much damage is to be applied!
			hit.rigidbody.SendMessageUpwards("ApplyDamage", hitPoints, SendMessageOptions.DontRequireReceiver);
		}
	}

}




renderer.material.color.a = 0.0;

yield new WaitForSeconds(initialDelay);

while (renderer.material.color.a < 1.0)
{
   color = renderer.material.color;
   color.a = 1.0;
   renderer.material.color = Color.Lerp(renderer.material.color, color, fadeSpeed * Time.deltaTime);
   
yield;

}
 
   // Destroy ourselves
   Destroy(gameObject);


   
   // Play a dying audio clip
   if (dieSound)
      AudioSource.PlayClipAtPoint(dieSound, transform.position);

	// Replace ourselves with a deadbody
	if (deadReplacement) {
		var dead : Rigidbody = Instantiate(deadReplacement, transform.position, transform.rotation);

		// For better effect we assign the same velocity to the exploded object
		dead.rigidbody.velocity = rigidbody.velocity;
		dead.angularVelocity = rigidbody.angularVelocity;
	}

At a rough guess – because you destroy the gameObject the script is living on in line one. (I could be wrong, but it’s a nice theory :slight_smile: A lot of things that would hose you in less forgiving environments “just work anyway” in Unity.)

I’m also guessing a glance at the console messages would give you a good clue.

Thanks for the reply, podperson. Problem is that the debugger is not returning any errors and seems to function correctly. I feel as if my bracketing is not in the right place or some other punctuation. Any others have an idea?

Well, the requirement is that the variable deadReplacement has an item inside it, so I’d check to make sure it exists. If it does exist, place Debug.Log markers after every chunk with a further number. If you know where it’s stopping, that should help the debugging.

If deadReplacement doesn’t exist nothing bad will happen.

If you’re not getting errors, I suspect it’s because nothing is assigned to dieSound.

Moving forward – I suggest you try to use consistent indentation in your code. Also, it’s pretty odd to basically shove everything in Start() and then use yield in a while loop.

The usual conventions are:

if( whatever ){
  x = y;
  if( something ){
    y = z;
  }
}

or:

if( whatever )
{
  x = y;
  if( something )
  {
    y = z;
  }
}

In either case it’s much easier to see what goes where.

Well yes, but if it doesn’t exist, the third condition that he wants to happen “3) Instantiate a new object with audio” won’t. Of course, the audio isn’t playing either, so that means the problem must occur earlier.

Try moving the destroy until after the sound and replacement.

It’s definitely an punctuation problem.

The curly braces just before the line

} // <- the problem



renderer.material.color.a = 0.0;

closes Start() and the remaining code is not part of any function.

I’m amazed this compiles at all :P.

Hi, guys, thanks very much for your time and effort helping me figure out this problem. Considering all of your comments, (especially Tomvds) who pointed out my egregiously messy punctuation :wink: and provided a suggestion which ultimately lead to a solution.
My still disorganized, cobbled script is below and works exactly as I need it to.

Thanks again for your help!

-Greg

var hitPoints = 100.0;
var deadReplacement : Rigidbody;
var dieSound : AudioClip;
var explosion : Transform;
var damagesound : AudioClip;
var initialDelay = 1.0;
var fadeSpeed = 1.0;


var explosionRadius = 5.0;
var explosionPower = 10.0;
var explosionDamage = 100.0;

var explosionTime = 1.0;

function Start () {
   
   var explosionPosition = transform.position;
   var colliders : Collider[] = Physics.OverlapSphere (explosionPosition, explosionRadius);
   
   for (var hit in colliders) {
      if (!hit)
         continue;
      
      if (hit.rigidbody) {
         hit.rigidbody.AddExplosionForce(explosionPower, explosionPosition, explosionRadius, 3.0);
                  
         var closestPoint = hit.rigidbody.ClosestPointOnBounds(explosionPosition);
         var distance = Vector3.Distance(closestPoint, explosionPosition);

         // The hit points we apply fall decrease with distance from the hit point
           var hitPoints = 1.0 - Mathf.Clamp01(distance / explosionRadius);
         hitPoints *= explosionDamage;

         // Tell the rigidbody or any other script attached to the hit object
         // how much damage is to be applied!
         hit.rigidbody.SendMessageUpwards("ApplyDamage", hitPoints, SendMessageOptions.DontRequireReceiver);
      }
   


renderer.material.color.a = 0.0;

yield new WaitForSeconds(initialDelay);

while (renderer.material.color.a < 1.0)
{
   color = renderer.material.color;
   color.a = 1.0;
   renderer.material.color = Color.Lerp(renderer.material.color, color, fadeSpeed * Time.deltaTime);
   
yield;

} 
   // Destroy ourselves
   Destroy(gameObject);


   
   // Play a dying audio clip
   if (dieSound)
      AudioSource.PlayClipAtPoint(dieSound, transform.position);

   // Replace ourselves with a deadbody
   if (deadReplacement) {
      var dead : Rigidbody = Instantiate(deadReplacement, transform.position, transform.rotation);

      // For better effect we assign the same velocity to the exploded object
      dead.rigidbody.velocity = rigidbody.velocity;
      dead.angularVelocity = rigidbody.angularVelocity;
   }}}