Help with prefabs instance

hi im trying to make an enemy using scripting, i have in a prefab and works great but when i put a second instance or a third or whatever number is doesn’t work anymore… basically the code is, if player punches the enemy then the moveSpeed of the enemy goes from 3 to cero and after 0.4 seconds it goes back to 3 again and if the enemy health is 0 or less calles the nowIsDead function.

Code:

using UnityEngine;
using System.Collections;

public class enemyHealthManager : MonoBehaviour {

    public int enemyHealth;

    public int enemyKnockback = 500;

    public bool isDead = false;


    Animator enemyAnim;

    private enemyPatrol enemy ;

    private fos_controller_script player ;





    // Use this for initialization

    void Start () {

        enemyAnim = GetComponent<Animator>();

        enemy = GameObject.FindGameObjectWithTag("enemy").GetComponent<enemyPatrol>();

        player = GameObject.FindGameObjectWithTag("Player").GetComponent<fos_controller_script>();


    }


    void FixedUpdate(){



    }

    // Update is called once per frame


    void Update () {
       

   



    }

    public void giveDamage(int damageToGive){


        GetComponent<AudioSource> ().Play ();

        enemyHealth -= damageToGive;

        if (enemyHealth >= 1) {
           
            StartCoroutine("freeze");
           
        }

        if (enemyHealth <= 0) {
           
            StartCoroutine("nowIsDead");
           
        }



       
       
    }

    public  IEnumerator freeze(){
           
   

        enemy.moveSpeed = enemy.moveSpeed * 0;

        GameObject.FindGameObjectWithTag ("enemyDeadSFX").GetComponent<AudioSource> ().Play ();
       
        yield return new WaitForSeconds (0.4f);
       
        enemy.moveSpeed = enemy.moveSpeed = 3;
       
       
       
    }
   
    public IEnumerator nowIsDead(){

           

            isDead = true;

            enemy.moveSpeed = enemy.moveSpeed * 0;

            enemyAnim.SetBool ("isDead", isDead);

            GameObject.FindGameObjectWithTag ("enemyDeadSFX").GetComponent<AudioSource> ().Play ();

            yield return new WaitForSeconds (0.3f);

            Destroy(gameObject);



    }

}

Im calling the script of enemyPatrol with is where the enemy takes the movement to put the moveSpeed to 0 and 3 again, again this works fine for one object but when i put more objects it only still works with one, even if the player punches one enemy the other instances (for example) stop. thank you and ill be watching.

Don’t use GameObject.FindGameObjectWithTag(“enemy”)
Instead add this script to each instance of an enemy. So each enemy game object now contains this script. Now you just call GetComponent(); and it will control the particular enemy game object that this particular instance of the script is attached to.

1 Like

Thanks Kurius this worked just fine, now i can put all the prefabs that i want and all of them are independent and works great!!! Thanks a lot!!!