Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Unity 2D Game

A topic by CHIP303 created Jun 19, 2020 Views: 315 Replies: 3
Viewing posts 1 to 4

Hello everyone, first, I want to say that Im sorry but my english is not the best and I hope you will understand what Im about to ask.
I started working on my first project ever (Soo, still learning), and I got into problem.
I was coding enemy and everything is good, I watched some video on yt, actually, few of them, trying to find answer but I couldn't.
When I start game, my enemy wont get his current health up to 100, it says "maxHealth 100, currentHealth 0" in debug mode, and he die with just one hit even if my attack damage is 25.
Well, here is my code:


public class Enemy : MonoBehaviour
{
    public Animator animator;
    public int maxHealth;
    int currentHealth;
    // Start is called before the first frame update
    void Start()
    {
        currentHealth = maxHealth;
    }
 
    public void TakeDamage(int damage)
    {
        currentHealth -= damage;
        //play hurt animation 

        animator.SetTrigger("Hurt");

        if(currentHealth <=0)
        {
            Die();
        }
    }

    void Die()
    {
        Debug.Log("Enemy Dead!");
        animator.SetBool("IsDead", true);

Thanks everyone.

Moderator moved this topic to General Development
(+1)

I'm sure your calling the TakeDamage in the enemy collision detection, correct? Maybe the bug is in that code.

(2 edits) (+1)

A couple of suggestions: I see you have a log statement in Die(), so you could add some to the Start and TakeDamage functions too so you can check the Console and make sure they're called in the manner you expect. If the Start function is not being called, make sure the script component is enabled.

(+1)

Thank you man, I didn't enable script component.... such a stupid mistake hahahahaha :3
I'm going to move to next goal in my project, if I get some more questions I will ask on this post here.
Thank you all again ^_^