So I'm making a FPS Survival game. I made a GameManager script which handles the Enemy Spawning part of the game. My EnemyAttack script handles the enemies attacking. Problem is, when the first enemy spawns, I only take 1 damage (as set in AttackDamage), but when the second spawns, I take 2 damage (AttackDamage * 2) even if I only get into attack range of 1 enemy. Here are all my enemy scripts:
EnemyAttack.cs:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class EnemyAttack : MonoBehaviour
{
public float AttackDamage = 1.0f;
public float AttackSpeed = 1.0f;
void Start() {
StartCoroutine(Attack());
}
IEnumerator Attack() {
while (true)
{
while (PlayerMovement.isEnemyAttacking == true) {
EnemyAttacking();
yield return new WaitForSeconds(AttackSpeed);
}
yield return 0;
}
}
public void EnemyAttacking() {
Debug.Log("Enemy Attacking!");
PlayerHealth.Health -= AttackDamage;
GameObject.FindGameObjectWithTag("HealthPoints").GetComponent<Text>().text = "HEALTH: " + PlayerHealth.Health;
}
}
EnemyAI.cs:
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform TriggerBox;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
if (EnemySight.inSight == true)
{
NavMeshAgent agent = GetComponent<NavMeshAgent>();
agent.destination = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>().position;
}
}
}
EnemySight.cs:
using UnityEngine;
using System.Collections;
public class EnemySight : MonoBehaviour {
public static bool inSight = false;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
inSight = true;
}
}
void OnTriggerExit(Collider other) {
if (other.gameObject.tag == "Player")
{
inSight = false;
}
}
}
and PlayerHealth.cs:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public Transform Player;
public static float Health = 100;
public static float maxHealth = 100;
// Use this for initialization
void Start () {
GameObject.FindGameObjectWithTag("HealthPoints").GetComponent<Text>().text = "HEALTH: " + Health;
}
// Update is called once per frame
void Update () {
if (Health <= 0) {
Dead();
}
}
public static void Dead() {
}
}