KiloKawse
KiloKawse

Reputation: 3

Public function from another script isn't called in Unity2D

I am trying to make an attack mechanic for my 2D game. It would work as follows: if the player was standing in front of the enemy and pressed space the enemy would die. To do that I wrote this in the player script:

    void Attack()
{
        Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(AttackArea.position, AttackRange, EnemyLayers);
        foreach (Collider2D enemy in hitEnemies)
        {
            enemy.GetComponent<Enemy_1>().Death();
            
        }
  }

And this in the enemy script:

    public void Death()
    {
        Destroy(gameObject);
    }

It worked flawlessly but I wanted to make another type of enemy to the game so I added this for the second enemy in the player attack script (I have mention that "GunBoi" is the name of the second enemy script and that I named the second function "Dead()" instead of "Death()" since I thought it wouldn't work if they had the same name)

Collider2D[] hitEnemies2 = Physics2D.OverlapCircleAll(AttackArea.position, AttackRange, EnemyLayers);
        foreach (Collider2D enemy2 in hitEnemies2)
        {
            enemy2.GetComponent<GunBoi>().Dead();
        }

and this in the second enemy script:

    public void Dead()
    {
        Destroy(gameObject);
    }

but whenever I try to attack the second enemy nothing happens. When I try to attack the first enemy it does work and the enemy dies. Does anyone know why this is happening?

Upvotes: 0

Views: 65

Answers (2)

Guilherme Umemura
Guilherme Umemura

Reputation: 323

About your problem: your code is in order. Check if the script have been assigned to the enemy prefab.

Now, a recommendation. If you follow it, your problem may be solved.

You said that you created a new script for the new enemy but they both share one same mechanics: the Death() function (and, as long as they are both enemies, i belive they will share a lot of other mechanics like HP, damage, and other traditional stuff).

In this case, i think that the best solution is to the GunBoi's script be inherited from the common enemy's one.

Replicate a function that do exactly the same thing but have a (not very) different name is a bad practice. If you decide to create a third enemy, you will have to create a third function to destroy it, but again, with a different name.

Upvotes: 2

abousquet
abousquet

Reputation: 586

Multiple things could be wrong, but at first sight I would at least verify those two:

  1. are the GunBoi on the good layer
  2. Do they have the GunBoi component

We lack too much information to help you further than this.

But honestly you should try to refactor this by having both enemy type have a generic enemy component. There is no reason why should would have to do the collision logic twice. You want to have to write as less code as possible. With your approach you will have to do this for each new enemy type which is both problematic and error prone.

Upvotes: 0

Related Questions