Wrong gameObject's health variable decrementing,not the one colliding.
// script attached to an empty gameobject
public void Damaging(int damage)
{
enemyHealthScript.currentHealth -= damage;
}
// script attached to enemy prefab
public int maxHealth;
public int currentHealth;
private void Start()
{
currentHealth = maxHealth;
}
private void Update()
{
if(currentHealth <= 0)
{
Destroy(gameObject);
return;
}
}
// script attached to the player colliding with the enemy
private void OnTriggerEnter2D(Collider2D collision)
{
speed = 0f;
InvokeRepeating("DamageWhenColliding", 0f, 1f);
}
private void DamageWhenColliding()
{
damageScript.Damaging(1);
}
private void OnTriggerExit2D(Collider2D collision)
{
target = null;
speed = 150f;
CancelInvoke("DamageWhenColliding");
Debug.Log("coll exit");
I think i didnt miss anything related to the damage logic.So what happens in my game: Example 1: Only one enemy in scene,everything works fine. Example 2: Two enemies in scene,player collides with the first one in the scene hierarchy "enemy" but he wont lose health,only the second "enemy (1)" loses health which is not even colliding. And if I add a third enemy,only the third one loses health no matter who i collide with,than the other 2 never lose health even if colliding.Sry if its a bad explanation of whats going on.
Also one extra question,how could i learn to write better logic for games.how could i practice that in an efficient way?
I am having a very similar issue and haven't been able to find any solid answer. If anyone can help, or even just point us in the right direction, I know I would be very grateful. $$anonymous$$y issue is a little different, as my objects only will explode in the order the are spawned when I add health to the enemy. If the player tries to shoot objects that spawn in second or third, it will blow up the first, second then third first, dropping their health once they get to the first objects position. Not sure if that makes any sense or not.
Your answer
Follow this Question
Related Questions
Apply Damage Universally 0 Answers
Health bar help 1 Answer
Damage indicator representing incoming damage direction with arrow in UI 2 Answers
Sending Damage and Damage Type Together?? 0 Answers
why wont my player take damage in Unet 0 Answers