Basically what I am doing is in my game if my character walks on a box he triggers an enemy to move across the screen. Problem is that if I kill the enemy and walk on that object again I get an error saying that the gameobject is destroyed and unity basically cannot call it. how do I tell unity to ignore this if the game object is destroyed?
Enemy Script
var speed : float=5.0;
var lifeTime = 3.0;
var health = 100;
var domove : int = 0;
function Awake () //unity predifined function
{
//Destroy (gameObject, lifeTime);//delete fireballs in "lifetime"/time set can also be changed from unity
}
function applydamage(damage:int)
{
Debug.Log("in applydamage"+health);
health = health-damage;
if (health ==0)
{
Destroy(gameObject);
Debug.Log("newhealthis" +health);
}
}
function Update()
{
if (domove==1)
{
transform.Translate(Vector3(0,0,speed)*Time.deltaTime);
//Debug.Log("update");
animation.Play("soldier_run");
/*audio.clip = walk;
audio.Play("runbitch");*/
}
}
function move () {
if (health >=1)
domove =1;
else
domove=0;
//transform.Translate(Vector3(0,0,speed)*Time.deltaTime);
//animation.Play("soldier_run");
}
Box trigger code:
#pragma strict
var speed : float=5.0;
var enemy : GameObject;
var walk : AudioClip;
function Start()
{
Debug.Log("start");
enemy = GameObject.FindWithTag("enemy");
Debug.Log("enemy is" + enemy.tag);
//Debug.Log(GameObject.Find
//enemy.SendMessage("move");
}
function OnTriggerEnter(other : Collider)
{
if(enemy)
Debug.Log("ouch");
//enemy = GameObject.Find("swat");
//Debug.Log("enemy is" + enemy.ToString);
enemy.SendMessage("move",SendMessageOptions.DontRequireReceiver);
audio.clip = walk;
audio.Play();
//GameObject.Find("swat").SendMessage("move");
//Debug.Log(enemy.ToString);
//enemy.
//enemy.transform.Translate(Vector3(0,0,(speed*Time.deltaTime)));
/*{
enemy.transform.Translate(Vector3(0,0,(speed*Time.deltaTime)));
//animation.Play("soldier_run");
}*/
}
Dunno if I need to further explain the scenario if so please ask and I will inform:)