My enemy don't take damage.
Hello to you ! I have a problem, my enemies refuse to die....(they are spawn and not already instanciate). I think it's because i can't get his current life from his script. But I tried everything and i can't get a component from "FindGameObjectsWithTag". Those are my script:
public class s_PlayerControl : MonoBehaviour
{
public int FoeCurrentHealth;
void Update()
{
GestureAttack();
FoeCurrentHealth = GameObject.FindGameObjectsWithTag("Foe").GetComponent<s_EnnemyControl>().FoeCurrentHealth;
}
GameObject GestureAttack()
{
invincible = true;
GameObject[] Foes = GameObject.FindGameObjectsWithTag("Foe");
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in Foes)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
if (recupgeste == "D" && recupgestescore > 0.60 || Input.GetKeyDown(KeyCode.Space))
{
transform.position = Vector3.MoveTowards(transform.position, closest.transform.position, 100);
//THE PROBLEM SOURCE (TOOK is log but not HIT)
s_PlayerControl enemyHealth = GetComponent<s_PlayerControl>();
Debug.Log(enemyHealth.FoeCurrentHealth);
if (enemyHealth.FoeCurrentHealth > 0)
{
Debug.Log("TOOK");
enemyHealth.TakeDamage(attackDamage);
}
}
return closest;
}
}
And for the enemy
public class s_EnnemyControl : MonoBehaviour
{
public int FoeCurrentHealth;
public int startingHealth = 1;
void Start()
{
FoeCurrentHealth = startingHealth;
}
public void TakeDamage(int amount)
{
Debug.Log("HIT");
// If the enemy is dead...
if (isDead)
// ... no need to take damage so exit the function.
return;
FoeCurrentHealth -= amount;
// If the current health is less than or equal to zero...
if (FoeCurrentHealth <= 0)
{
// ... the enemy is dead.
Death();
}
}
}
Answer by deglet · May 04, 2016 at 09:20 AM
It's not my problem, they are tagged. I get their position previously. No, I can't damage them. Their life is staying at 1point whatever I do.
GameObject GestureAttack()
{
invincible = true;
GameObject[] Foes = GameObject.FindGameObjectsWithTag("Foe");
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
s_PlayerControl enemyHealth = GetComponent<s_PlayerControl>();
foreach (GameObject go in Foes)
{
FoeCurrentHealth = go.GetComponent<s_EnnemyControl>().FoeCurrentHealth;
Debug.Log(FoeCurrentHealth);
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
if (recupgeste == "D" && recupgestescore > 0.60 || Input.GetKeyDown(KeyCode.Space))
{
transform.position = Vector3.MoveTowards(transform.position, closest.transform.position, 100);
// foreach (GameObject go in Foes)
//{
Debug.Log(enemyHealth.FoeCurrentHealth);
if (FoeCurrentHealth >0)
{
Debug.Log("TOOK");
enemyHealth.TakeDamage(attackDamage);
//}
}
}
return closest;
}
As I say the TOOK is log, and it's log that they have 1 point of life. But the TakeDamage don't work. Even if I do enemyHealth.FoeCurrentHealth = enemyHealth.FoeCurrentHealth -1; They still have all their life.
Your answer
Follow this Question
Related Questions
How to get my TakeDamage script to hit/get other components? 0 Answers
How can I get my enemy to hurt/kill player 0 Answers
How to go on to next wave of enemies? 0 Answers
Damage not working properly 1 Answer