Hey everyone. I have AI enemies and some weapon meshes. These meshes have C# scripts attached to them that damage the enemies. But, there’s only one target (as a GameObject) it can lock on to. I’m using arrays (not List<>, just arrays) to handle this, but I want it to remove an object from the array when it dies. Is there a way to that? Thanks.
Here’s one of the scripts:
using UnityEngine;
using System.Collections;
public class HammerDamage : MonoBehaviour {
public GameObject[] targets;
public GameObject target;
public float attackTimer;
public float coolDown;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 2.0f;
// targets = GameObject.FindGameObjectsWithTag("Enemy 3").Select(g=>g.transform).ToList();
}
// Update is called once per frame
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(Input.GetKeyUp(KeyCode.F)) {
if(attackTimer == 0) {
HammerAttack();
attackTimer = coolDown;
}
}
}
public void HammerAttack() {
GameObject[] gos = GameObject.FindGameObjectsWithTag("Enemy 3");
targets = gos;
// target = gos;
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 < 2f) {
if(direction < 0) {
EnemyHitPoints ehp = (EnemyHitPoints)target.GetComponent("EnemyHitPoints");
ehp.AdjustCurrentHitPoints(-45);
// EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
// eh.AddjustCurrentHealth(-45);
}
}
}
}
(if post doesn’t look right, please edit)