As of now I have a homing missile that fires when an enemy object is clicked. I am having an issue once the enemy is destroyed. If I have multiple missiles tracking the same enemy I receive and error when one missile destroys the enemy while others are still tracking it. I tried to check to see if target obj is null and if it is then destroy the missiles but this does not seem to work. I am not sure what I am doing wrong here. This script is attached to the missile.
`var targetObj : Transform;
var shipExplode : Transform;
var lifeTime : float = 5;
var missileSpeed : float = 100;
var enemySound : AudioClip;
function Awake()
{
Destroy(gameObject, lifeTime);
}
function Update ()
{
//this is the code part that I am trying to figure out
//if you can let me know what I am doing wrong that would be great
//the error that I get when after the target is destroyed is
//the object of type ‘Transform’ has been destroyed but you are still trying
//to access it. script should check for if it is null
if(targetObj.transform == null)
{
Destroy(gameObject);
}
transform.LookAt(targetObj);
transform.Translate(Vector3.forward * missileSpeed * Time.deltaTime);
}
function OnTriggerEnter()
{
destroyObjects();
}
function destroyObjects()
{
var missilePosition : Vector3 = transform.position;
var shipPosition : Vector3 = targetObj.transform.position;
audio.PlayOneShot(enemySound);
Destroy(gameObject);
Destroy(targetObj.gameObject);
GlobalVars.score++;
var shipClone : Transform;
shipClone = Instantiate(shipExplode, shipPosition, Quaternion.identity);
Destroy(shipClone.gameObject, lifeTime);
}
`