Search Unity

Accesing another Script for Damage values

Discussion in 'Scripting' started by MrZeker, Feb 12, 2019.

  1. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    106
    Hello
    Im using a fairly simple script, where when any enemy hits with a OnTriggerEnter the player (or the enemy) takes damage, however, the script is fixed to 10 damage always.
    How can i make so that every enemy can deal its damage and not always a static 10.
    I tought of using a public variable on each enemy, but i have no clue how to reference that variable, most tutorials or questions just allow for a -system- to damage a or be damaged, but not how to assign a different damage for every enemy.
    I saw a solution to just try to put a public variable to reference the enemy, but that would be extremely heavy if i want to reference 50+ enemies i think.

    This is the part of my playerhealth script where i take damage if enemyweapon collides with player.

    Code (csharp):
    1.  
    2.     void takeHit ()
    3.     {
    4.                 if (currenthealth >= 0)
    5.         {
    6.             GameManager.instance.PlayerHit(currenthealth);
    7.             anim.Play("Hurt");
    8.             currenthealth -= 10  ;
    9.            
    10.             healthSlider.value = currenthealth;
    11.             audio.PlayOneShot(hurtAudio);
    12.         }
    13.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,561
    You should have a script on the enemy for how much damage it deals. Then on the OnTriggerEnter, you can get that enemies script and pull that value to apply the damage.

    Or, could have the enemy call a DealDamage method on the player and pass in the damage it is doing.

    Just a few ideas...
     
  3. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    106
    that's exactly what i want to do but i dont know how to do it :(
    i created the enemyStrength variable, but i have no idea how to make the playerhealth script read that variable value.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,561
    Look into the OnTriggerEnter method. There are tons of examples of the use of it and getting the gameObjects involved in the triggering.

    Then you can just use GetComponent off the enemy.

    https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html

    Code (CSharp):
    1. private void OnTriggerEnter(Collider other)
    2.     {
    3.         float damageAmount = other.GetComponent<Enemy>().damageAmount;
    4.     }
    Something along those lines. Again, this is pretty common and should be easy to find better examples. This is from the player side, but you might have the weapon do a check if it hit the player and deals damage instead of having the player retrieve damage.
     
  5. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    106
    Sorry for being so new, thats what i got, but i get an error of Null Reference Exception type, i Added a

    Code (csharp):
    1.  
    2. public int damageAmount = 50;
    3.  
    on the enemyAttack script. but it seems it cant read it? im not sure really :(

    Code (csharp):
    1.  
    2.   private void OnTriggerEnter(Collider other)
    3.     {
    4.         if (timer >= TimeSinceLastHit && !GameManager.instance.GameOver)
    5.         {
    6.             if (other.tag == "Weapon")
    7.             {
    8.  
    9.     int damageAmount = other.GetComponent<EnemyAttack>().damageAmount;
    10.                
    11.         if (currenthealth >= 0)
    12.         {
    13.             GameManager.instance.PlayerHit(currenthealth);
    14.             anim.Play("Hurt");
    15.             currenthealth -= damageAmount
    16.                 timer = 0;
    17.             }
    18.         }
    19.     }
    20.  
    21. ;
    22.  
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,561
    If you're getting a null error on line 9, it means it's not finding the EnemyAttack script on "other". So you'll need to make sure your components are properly attached, otherwise depending on how you have it setup you might have to use the other GetComponent calls.
     
    MrZeker likes this.
  7. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    106
    Yay, thanks! i needed to use the GetComponentInParent (since the ontrigger was checking for the boxcollider on the attacker's fist!)
    Thank you man!!!!
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,561
    Glad you got it working!
     
    MrZeker likes this.