Ok. So I made an enemy and made a few copies of it. Everything seemed fine. The enemies were following my character, but after I killed one of them and then clicked again to initiate another attack the game ended and I got this error.
MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
Here is the code I am using for the PlayerAttack.
using UnityEngine;
using System.Collections;
public class PlayerAttack : MonoBehaviour {
public GameObject target;
public float attackTimer;
public float coolDown;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 0.2f;
}
// Update is called once per frame
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(Input.GetKeyUp(KeyCode.Mouse0)){
if(attackTimer == 0){
Attack();
attackTimer = coolDown;
}
}
}
private void Attack(){
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
Debug.Log (direction);
if(distance < 2.5){
if(direction > 0){
EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
eh.AddJustCurrentHealth(-10);
}
}
}
}
I have no idea how to work around this.