Question by 
               The-IT-Nation · Jul 23, 2019 at 03:26 PM · 
                scripting problemscript.programming  
              
 
              1 script for multiple enemy
i am referencing enemy script from player script and then when enemy is in range i can attack him , but when i drag multiple enemies i can attack only the referenced script but not others
               Comment
              
 
               
              Each enemy has a personal copy of that script. If you have 10 enemies, there are 10 copies. The player points to one particular copy, on one particular enemy.
For a hint how to find different enemies to point at, maybe look up "find nearest enemy".
Answer by The-IT-Nation · Jul 24, 2019 at 03:52 PM
@Cornelis-de-Jager @aditya23dedhia Enemy Code
 public class Enemy : MonoBehaviour
 {
     public float health = 100f;
     public float walkSpeed = 1.0f;      
     public float wallLeft = 0.0f; 
     public float wallRight = 5.0f;
     public float walkingDirection = 1.0f;
     Vector2 walkAmount;
     float originalX;
     public GameObject deathParticles;
     public Player Player;
     CircleCollider2D collid;
     public float waitTime = .5f;
     public bool playerInRange;
     void Start()
     {
         playerInRange = false;
         collid = GetComponent<CircleCollider2D>();
         this.originalX = this.transform.position.x;
     }
 
     void Awake() {
         
     }
 
     
     void Update() {
 
         if (walkingDirection > 0.0f && transform.position.x >= originalX + wallRight) {
 
             walkingDirection = -1.0f;
             Vector2 temp = transform.localScale;
             temp.x *= walkingDirection;
             transform.localScale = temp;
 
         } else if (walkingDirection < 0.0f && transform.position.x <= originalX - wallLeft) {
 
             walkingDirection = 1.0f;
             Vector2 temp = transform.localScale;
             temp.x *= -walkingDirection;
             transform.localScale = temp;
 
         }
 
         walkAmount.x = walkingDirection * walkSpeed * Time.deltaTime;
 
         transform.Translate(walkAmount);
 
         if (health <= 0)
         {
             Death();
         }
 
     }
     void Death() {
         Instantiate (deathParticles, transform.position, transform.rotation);
         Destroy(gameObject);
     }
 
 
     void OnCollisionEnter2D(Collision2D other) {
         if (other.gameObject.tag == "Player") {
             Player.Death();
         }    
     }
     
 }
Player Code
 public class Player : MonoBehaviour
 {
     public CharacterController2D controller;
     public Enemy enemy;
     public float runSpeed;
     public float crouchSpeed;
     public float whileAttackSpeed;
     public float damage;
     public GameObject deathParticles;
     //public ParticleSystem walkingParticles;
     [System.NonSerialized]public bool dead;
     bool jump;
     bool run;
     bool crouch;
     bool attack;
     bool runB;
     bool enemyIsInRange;
     bool grounded;
     float runSpeedBak;
     float hMove;
     int indt;
     Animator anim; 
     Rigidbody2D rb;
     float waitBeforeAttack = 1.5f;
     void Start()
     {
         enemyIsInRange = false;
 
         rb = GetComponent<Rigidbody2D>();
 
         anim = GetComponent<Animator>();
 
         run = false;
 
         dead = false;
 
         jump = false;
 
         crouch = false;
         grounded = true;
 
         runSpeedBak = runSpeed;
     }
 
     
     void Update()
     {
         
 
         if (Input.GetKeyDown(KeyCode.Space)) {
 
             jump = true;
 
             anim.SetTrigger("Jump");
 
             grounded = false;
 
         }
 
         if (Input.GetKeyDown(KeyCode.Z)) {
 
             anim.SetTrigger("Attack");
 
             attack = true;
 
             rb.constraints = RigidbodyConstraints2D.FreezeAll;
 
             if (enemyIsInRange == true) {
                 
                 enemy.health -= damage;
                 Debug.Log(enemy.health);
 
             }
         }
 
         
         if (Input.GetKey(KeyCode.C)) {
 
             crouch = true;
 
             runSpeed = crouchSpeed;
 
             anim.SetBool("Crouch", true);
 
         } else {
 
             if (Input.GetKeyUp(KeyCode.C)) {
 
                 crouch = false;
 
                 runSpeed = runSpeedBak;
 
                 anim.SetBool("Crouch", false);
 
             }
             
         }
 
         hMove = Input.GetAxisRaw("Horizontal");
         
 
         if (hMove != 0) {
 
             run = true;
 
             anim.SetBool("IsRunning", true);
 
         } else {
 
             run = false;
 
             anim.SetBool("IsRunning", false);
 
         }
 
         
     }
 
     void FixedUpdate() {
         
         controller.Move(hMove * runSpeed * Time.deltaTime, crouch, jump);
 
         jump = false;
 
     }
 
     public void Death() {
 
         Instantiate (deathParticles, transform.position, transform.rotation);
         
         Destroy (gameObject);
 
         dead = true;
         
     }
 
     public void OnLandEvent() {
         grounded = true;
 
        //anim.Play("Idle");
        //anim.SetBool("JumpB", false);
 
     }
 
     void OnTriggerEnter2D(Collider2D col) {
 
         GameObject objectCollided = col.gameObject;
         if (objectCollided.CompareTag("Enemy")) {
             
             enemyIsInRange = true; 
         }
     }
     void OnTriggerExit2D(Collider2D col) {
 
         if (col.tag == "Enemy") {
 
             enemyIsInRange = false;
 
         }
     }
 
     public void attackOver() {
 
         rb.constraints = RigidbodyConstraints2D.FreezeRotation;
 
         attack = false;
 
     }
 
 }
 
 
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                