Enemy is dead even though nothing happened

I made a script for the enemy and when the health = 0. The problem is that it starts with 20 health but apparently the health drops to 0 at the start. There is a function that would remove 10 health from the health variable when hit, the enemy did not get hit and already the health depletes to 0.

Enemy Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour {

    public static bool eh;
    public float health = 20;
   
    Animator anim;

    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator>();
    }
   
    // Update is called once per frame
    void Update () {
        bool dead = false;
        eh = Player.hit;

        Debug.Log(health);

        if (eh == false)
        {
            anim.Play("Enemy Stand");
        }

        if (eh == true)
        {
            StartCoroutine(hit());
            health -= 10;
        }

        if (health == 0)
        {
            dead = true;
            Debug.Log(dead);
        }

    }

   
    IEnumerator hit()
    {
        yield return new WaitForSeconds(2);
        anim.Play("Enemy_hit");
        //yield return new WaitForSeconds(1);
        eh = false;

    }
   
}

player script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {

    private static bool movement;
    private static bool attk_move;
    private static bool moveback;
    public static float player_health;
    public static bool hit;

    Animator anim;

    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator>();
       
    }
   
    // Update is called once per frame
    void Update () {

        movement = Attack.move;
        attk_move = Attack.attk;
        moveback = Attack.moveback;

        if(movement == true)
        {
            StartCoroutine(mf());
        }

        if (attk_move == true && moveback == false)
        {
            StartCoroutine(attack());
        }

        if (attk_move == false && moveback == true)
        {
            StartCoroutine(mb());
        }

        if(movement == false && attk_move == false)
        {
            anim.Play("Stand");
        }

    }

    IEnumerator mf()
    {
        anim.Play("Walk FOrward");
        transform.Translate(Vector2.left * 5f * Time.deltaTime);
        yield return new WaitForSeconds(5);
    }

    IEnumerator attack()
    {
        anim.Play("Attack");
        hit = true;
        print("Hit");
        yield return new WaitForSeconds(1);
        hit = false;
    }

    IEnumerator mb()
    {
        transform.Translate(Vector2.right * 5f * Time.deltaTime);
        yield return new WaitForSeconds(5);
        //transform.Translate(Vector2.left * 0f * Time.deltaTime);
    }

}

An issue I see is this:

if (eh == true)
        {
            StartCoroutine(hit());
            health -= 10;
        }

But the coroutine is:

IEnumerator hit()
    {
        yield return new WaitForSeconds(2);
        anim.Play("Enemy_hit");
        //yield return new WaitForSeconds(1);
        eh = false;

    }

You’re setting eh to false after 2 seconds, but you’re doing health-=10 in the Update function. You should do this:

[code=CSharp]if (eh == true)
        {
            StartCoroutine(hit());
            //set eh as false so you don't do -=10 more than once per "eh==true"
            eh = false;
            health -= 10;
        }

Is eh “true” in the Inspector when you start the game? In Start, you can make Eh false for good measure.

And what kind of bool name is “eh”?

thanks for the reply, I will try your method out asap. By the way eh stands for enemy health, I should be using comments when I program but I usually forget.

eh => enemyHealth
mf() => MoveForward()
mb() => MoveBackwards()

Don’t do this. Really bad habit that makes reading your code really tricky and with autocompletion in pretty much all IDEs there really is no need for it.

http://www.dofactory.com/reference/csharp-coding-standards

yup, when you get on to building more lengthy projects you’ll be amazed at how easy it is to forget what the code you wrote last month is doing in detail when you need to come back and tweak it.

1 Like

Comments don’t replace the need for descriptive variable names. Use comments when you need them, but don’t use them to compensate for not naming things properly.

3 Likes
[code=CSharp]if (eh == true)
        {
            StartCoroutine(hit());
            //set eh as false so you don't do -=10 more than once per "eh==true"
            eh = false;
            health -= 10;
        }

I tested your method and now the health of the enemy stays at 20 but it created another problem. When the player hits the enemy, the health doesn’t decrease at all. I used Debug.Log(); to check if the enemy was getting hit or not and when I tried it out, the enemy did get hit but the health didn’t decrease.

Why are you setting eh to false before subtracting the health? Of course it doesn’t work…