- Home /
Having issues getting player to damage multiple enemies
I have my PlayerAttack script set up to only be able to damage instantiated enemies within melee range, however the player will only damage the most recent instantiated enemy, and all other instantiated enemies ignore the script.
Looking for a way to fix probably my "target" variable to make it so I can damage any enemy that gets within range, no matter the order of instantiation. My "target"s are prefabs, not instances, if that adds any info.
I've tried using FindObjectsOfType<EnemyAI>()
but it's not working with target being classed as a Transform, nor a GameObject, and I'm not exactly sure what else to refer to it as.
EnemyHealth eHealth;
float eDamage = 30f;
Transform target;
float damageDistance;
float damageRange = 1f;
void Update()
{
target = FindObjectOfType<EnemyAI>().transform;
damageDistance = Vector3.Distance(target.transform.position, gameObject.transform.position);
eHealth = FindObjectOfType<EnemyHealth>();
if (Input.GetKey(KeyCode.Q))
{ GetComponent<Animator>().SetBool("Attack", true); }
else
{ GetComponent<Animator>().SetBool("Attack", false); }
}
public void AnimationDamage()
{
if (damageDistance <= damageRange)
{
eHealth.TakeDamage(eDamage);
}
}
Another side note, I'm tacking on NullReferenceExceptions on my "target" in Update() up until the point of my first enemy instantiating, since FindObjectOfType is returning null until an enemy shows up. Any way to stop those errors from popping up?