Search Unity

Why do all the enemies in my scene take damage when I damage just one?

Discussion in 'Scripting' started by farazk86, Nov 24, 2019.

  1. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    195
    Hi,

    So I have a script spawning enemies in my scene. Every enemy game object has an enemy script on it that detects a tap or mouse click.

    When a click on enemy is detected the health decrements and if it goes below 0 that gameObject is destroyed.

    Code (CSharp):
    1. void Update()
    2.     {
    3.        
    4.         if (Input.GetMouseButtonDown(0))
    5.         {
    6.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    7.             RaycastHit hit;
    8.  
    9.             if (Physics.Raycast(ray, out hit))
    10.             {
    11.                 if (hit.transform.tag == "Enemy")
    12.                 {
    13.                     health--;
    14.                     if (health <= 0)
    15.                         Destroy(gameObject);
    16.                 }
    17.             }
    18.         }
    19.     }
    The problem is that when I click on an enemy, every enemy in the scene takes damage rather than just the one I am clicking on.

    Can't figure out why this is.

    Any ideas?

    Thanks
     
  2. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    426
    Code (CSharp):
    1.                if (hit.transform.tag == "Enemy")
    2.                 {
    3.                     health--;
    4.                     if (health <= 0)
    5.                         Destroy(gameObject);
    Is this bit reducing the health of everything with the Enemy tag globally? It doesn't seem like it's singling out the hit.transform it is hitting?
     
  3. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Health seems to be declared on the player, so naturally that will effect all enemies. It should be declared on the enemy and decreased when you hit it
     
    farazk86 likes this.
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    @farazk86 what is happening in your script is, when ray hits any object, every object checks for same ray. In your case, doesn't matter which object is being hit, as long at least one is hit. Further each object reduces its health.

    OnUpdate aimply go through every script attached to game objects.

    What you would need, to check if hit collider, transform, or game object (hash), is the same, as as the object, to which script is attached to.

    Problem is, you run ray collision check on every game object per frame. You should really run one ray check in frame and compare hit results.

    Simply, create single script, check ray collision, use for loop to iterate through all related objects, and check if hit object is same as the any of looped object. If so, reduce object's health.

    In such case, you need access health reference of the hit object.
     
    farazk86 likes this.