So, i wanted to make an zombie survival game with infinite waves and i have made an attack system where if i enter an certain collider on the enemy, it will play its attack animation and it’s hitbox will be activated. Now the problem is that when i duplicate the enemy the script or function (not sure) only works on the main enemy, not the copy. And whenever i destroy the “main” enemy i get an error leading to the attack area script. The prefab is exactly the same and i used the instantiate function. If you don’t understand my problem yet, take a look at this video i made :
As you can see whenever i enter any of the duplicates “attack area” the animation only plays on the main prefab.
Here is the Attack Area script:
using System.Collections.Generic;
using UnityEngine;
public class AttackArea : MonoBehaviour {
Animator animator;
DetectHit detectHit;
Collider collider;
void Start ()
{
animator = GameObject.FindGameObjectWithTag ("Enemy").GetComponent<Animator> ();
detectHit = GameObject.FindGameObjectWithTag ("Hitbox").GetComponent<DetectHit> ();
collider = GetComponent<Collider> ();
}
void Update ()
{
}
void OnTriggerEnter(Collider collider)
{
detectHit.enabled = true;
if (collider.tag == "Player")
{
detectHit.enabled = true;
animator.SetBool ("isAttacking", true);
animator.SetBool ("isWalking", false);
animator.SetBool ("isDead", false);
}
}
void OnTriggerExit (Collider col)
{
if (col.tag == "Player")
{
detectHit.enabled = false;
animator.SetBool ("isAttacking", false);
animator.SetBool ("isWalking", true);
animator.SetBool ("isDead", false);
}
}
}
Any kind of help will be appreciated, thank you.