Enemy Prefabs

I have been searching the whole internet, and I confused about the prefabs.

Basically my player will attack(teleport) to the the enemy(prefab), but when I add the second enemy’s prefab. It only attack the first enemy and the death animation is not trigger while the second enemy cannot be detected by the player nor show death animation. But it works for one enemy.

This is my player Script

This is my attack script

This is when I only use 1 enemy

ufblcx

This is my enemy script

public class Enemy : MonoBehaviour
{
    public enum enemyState
    {
        alive,
        death
    }
    public enemyState currentState;
    public Animator enemyAnimator;
    public Transform player;


    void Start()
    {
        // enemyAnimator = GetComponent<Animator>();
        // idk = GameObject.Find("target").GetComponent<Movement>();
        // player = GameObject.FindWithTag("Player").transform;
       
    }

    public void beenKilled()
    {
        currentState = enemyState.alive;
       
        // if player pressed "space" button and the player has teleport and the enemy is currently alive then call the Die function
        if (Input.GetButtonDown("Attack") && player.position == transform.position && currentState != enemyState.death){
            Die();
        }
    }

    void Die()
    {
        Debug.Log("You are killed");

        // death animation
        enemyAnimator.SetBool("death", true);

        // change enemy state
        currentState = enemyState.death;

        // disable enemy collider to allow player to move and ignore the enemy corpse and disable the enemy script
        GetComponent<Collider2D>().enabled = false;
        this.enabled = false;
    }
}

This is my attack script

public class Attack : MonoBehaviour
{
    public Animator enemyAnimator;
    public Transform target;
    public float teleportRadius;
    public float dashDistance;

    void Start()
    {
        target = GameObject.FindWithTag("Enemy").transform;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // trigger hit function after pressed "space" button
            Hit();
        }
    }

    void Hit()
    {
        Vector3 change = GetComponent<Movement>().change;
        float speed = GetComponent<Movement>().speed;

        // attack animation
        enemyAnimator.SetTrigger("attTrigger");

        // Dash
        // transform.position += dashDistance * change * speed * Time.deltaTime;

        // check enemy in radius, if enemy in radius then dash(teleport) to the enemy position and kill the enemy
        if (Vector3.Distance(transform.position, target.position) < teleportRadius && target.GetComponent<Enemy>().currentState == Enemy.enemyState.alive )
        {
            // teleport
            transform.position = target.position;

            // call beenKilled funciton from Enemy script
            target.GetComponent<Enemy>().beenKilled();
        }

    }
}

Just from a first glance, it looks like you are only assigning target in start. So it only happens once when the script is initialized. That would make sense if it is only working for the first enemy

Thanks :), but fortunately it is still not working :confused:

Well what did you try to fix it?

1 Like

I move the target = GameObject… to the update. I don’t know if this the correct way.

I just added my problem’s video ,please check it

Interesting. I am not entirely sure, but try FindGameObjectsWithTag instead of FindGameObjectWithTag. That S means it will look for multiple. You may have to make target a List or Array, but we can get there eventually if this doesnt work. Throw in a Debug.Log(target.name) just after you assign target in Update to see what the name of the gameobject is. If after the first kill it stays the same or goes null, then you know it isnt working.

Try this video too if that solution doesnt work:

1 Like

Alright, thank you for your help. I change my script a little bit and it finally works. Thank you in advance

You need to assign a new target every time you press space to hit an enemy.
Make a function which will find the closest enemy to the player and assign it to the target.

1 Like

Yes , I have fixed my script.
So first I make a list to have gameobjects with enemy tag, and I iterate each to find the closest enemy to the player