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.
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;
}
}
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
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.
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.