How to I make a explode on Raycast?

I am making a helicopter game in unity and I want it so that the ship will explode once it’s health is down to zero. I have done some testing and my code doesn’t work and the explosion prefab just keeps playing. Here is my code…

#pragma strict

var Health = 100;
var ExplosionPrefab : GameObject;
var ExplosionSound = "";
var ShipPrefab : GameObject;
var hit : RaycastHit;
var foundhit : boolean = false;

function Start () 

function Update () 
	if(Health < 0);
		Instantiate(ExplosionPrefab, transform.position, transform.rotation);
	if(Physics.Raycast(transform.position, transform.forward))
		Health = Health - 1;

If there is something I am missing please tell me, this is a class project and it is due and the end of the week. That would be great if I could get some help.


To fully and correctly answer your question, I might need some more information about the explosion prefab(scripts attached, mostly). Add this to the ‘Start()’ method on your prefab’s script: Destroy(gameObject, 5), and according to the Unity Documentation, this script should destroy any gameobject that it’s attached to within 5 seconds of being activated.

Best of luck with your project and your class :wink:,