Search Unity

How to damage different enemies with same tag.

Discussion in 'Scripting' started by emirkoskenli, Oct 19, 2018.

  1. emirkoskenli

    emirkoskenli

    Joined:
    Oct 7, 2018
    Posts:
    5
    Greetings everyone. I'm pretty new at c# and unity and I'm stuck with this "damage" system. I have this code for my arrow prefab. It's seeking gameobjects with tagged "Enemy" and when enemy in my range, it's start shooting and giving damage to it.There is no problem till here. Problem is I have 2 enemy type with same tag and different scripts. If I write "orc1.health -= karakter.damage;" thats pretty work on orc1 but when i shoot the orc2, i had an error. any help will be appreciated.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Arrow : MonoBehaviour {
    7.  
    8.     private Transform target;
    9.     public float speed = 40f;
    10.     float randValue;
    11.    
    12.     public Orc1 orc1;
    13.     GameObject hasarver;
    14.  
    15.     public Orc1Boss orc1Boss;
    16.     GameObject bosshasarver;
    17.  
    18.     public Levelup levelup;
    19.     GameObject expver;
    20.  
    21.     public Karakter karakter;
    22.     GameObject damageUp;
    23.  
    24.     public GameObject FloatingTextPrefab;
    25.     public GameObject FloatingCritTextPrefab;
    26.  
    27.     public void Seek(Transform _target)
    28.     {
    29.         target = _target;
    30.     }
    31.  
    32.     void Start ()
    33.     {
    34.         hasarver = GameObject.FindGameObjectWithTag("Enemy");
    35.         orc1 = hasarver.GetComponent<Orc1>();
    36.  
    37.         bosshasarver = GameObject.FindGameObjectWithTag("Enemy");
    38.         orc1Boss = bosshasarver.GetComponent<Orc1Boss>();
    39.  
    40.         expver = GameObject.FindGameObjectWithTag("expbar");
    41.         levelup = expver.GetComponent<Levelup>();
    42.  
    43.         damageUp = GameObject.FindGameObjectWithTag("karakter");
    44.         karakter = damageUp.GetComponent<Karakter>();
    45.  
    46.         randValue = Random.value;
    47.     }
    48.    
    49.    
    50.     void Update ()
    51.     {
    52.        
    53.         if (target==null)
    54.         {
    55.             Destroy(gameObject);
    56.             return;
    57.         }
    58.  
    59.         Vector3 dir = target.position - transform.position;
    60.         float distanceThisFrame = speed * Time.deltaTime;
    61.  
    62.         if (dir.magnitude<=distanceThisFrame)
    63.         {
    64.             HitTarget();
    65.             return;
    66.         }
    67.  
    68.         transform.Translate(dir.normalized * distanceThisFrame,Space.World);
    69.  
    70.     }
    71.  
    72.     void HitTarget()
    73.     {
    74.         if (randValue>karakter.critChance)
    75.         {
    76.             var go = Instantiate(FloatingTextPrefab, transform.position, Quaternion.identity,transform.parent);
    77.             go.GetComponent<TextMesh>().text = karakter.damage.ToString();
    78.             Destroy(gameObject);
    79.             orc1.health -= karakter.damage;
    80.         }
    81.         else
    82.         {
    83.             var go = Instantiate(FloatingCritTextPrefab, transform.position, Quaternion.identity,transform.parent);
    84.             go.GetComponent<TextMesh>().text = karakter.critDamage.ToString();
    85.             Destroy(gameObject);
    86.             orc1.health -= karakter.critDamage;
    87.         }
    88.  
    89.     }
    90. }
    91.  
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,638
    Why is Orc1 a component. Are Orc1 and Orc2 two different types of orcs that have different behaviors? There are lots of different ways to solve this problem but I need to find out more about what you are trying to do.
     
    Joe-Censored likes this.
  3. SVKsuli

    SVKsuli

    Joined:
    Mar 23, 2014
    Posts:
    63
    I dont know if your enemy and arrow Gameobjects have Collider components but if yes then its simple:
    Code (CSharp):
    1.     private void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.         if(collision.transform.tag == "Enemy")
    4.         {
    5.             collision.transform.gameObject.GetComponent<EnemyBodyScript>().hp -= dmg;
    6.         }
    7.     }
    Its always best if all your enemies have same script for controling dmg.
     
  4. DasJonny

    DasJonny

    Joined:
    May 15, 2017
    Posts:
    32
    Tag every orc as "Enemy" and then you give every enemy a same script for controlling the HP.

    Your script should look smth. like this then:
    Code (CSharp):
    1. private void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.         if(collision.transform.tag == "Enemy")
    4.         {
    5.             collision.transform.gameObject.GetComponent<EnemyHPControlScriptt>().hp -= dmg;
    6.         }
    7.     }
    Its always the best to have the same script on every enemy for things like this
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    What's the purpose of having orc1 and orc2 scripts instead of the same script for both orcs?