Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Accessing sword damage from another scripts ? and dealing damage.

Discussion in 'Scripting' started by BluesyPompanno, Sep 1, 2017.

  1. BluesyPompanno

    BluesyPompanno

    Joined:
    Jan 1, 2017
    Posts:
    37
    Hello I need help.

    I have a player that has 2 childs - Sword and a SwordHitBoxCollisionObject.
    My player object has 2 swords that deal different amount of Damage ( 50 and 20 ).

    The Sword has this script

    Code (CSharp):
    1.    
    2. public Transform Ruka;
    3.     public int Damage;
    4.  
    5.     void Update()
    6.     {
    7.         transform.position = Ruka.position;
    8.         transform.rotation = Ruka.rotation;
    9.     }
    10.    
    11.  
    and the SwordHitBoxCollisionObject that gets enabled when the player attacks (This object is supposed to deal damage) and has script that has

    public int FinalDamage.

    then I have Enemy object that doesn't move.
    with this script

    Code (CSharp):
    1.  
    2. public float maxHEALTH = 100;
    3.     public float currentHEALTH;
    4.     public int amount;
    5.  
    6.     void Start()
    7.     {
    8.         currentHEALTH = maxHEALTH;
    9.  
    10.     }
    11.     void OnTriggerEnter(Collider other)
    12.     {
    13.         if (other.gameObject.tag == "Player")
    14.         {
    15.           currentHEALTH = currentHEALTH - amount;
    16.         }
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         if (currentHEALTH <= 0f)
    22.         {
    23.             DIE();
    24.         }
    25.     }
    26.     void DIE()
    27.     {
    28.  
    29.         Destroy(gameObject);
    30.     }
    My question is How do i get the Damage variable from the sword into the SwordHitBoxCollisionObject so that it would deal the same amount of damage to the enemy as in the Sword script ?

    I can't find a way how to do this.Creating public voids TakeDamage didn't worked for me.

    (Swords are controlled by the players animations = always folows hand)

    Anybody knows how to fix this ?
     
  2. GamerJoHo

    GamerJoHo

    Joined:
    Oct 29, 2016
    Posts:
    17
    Hi Bluesy,

    If you look at GetComponent, you should be able to use that:
    https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

    Essentially, you can get the sword from the collision and use that.

    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2.     {
    3.         if (other.gameObject.tag == "Player")
    4.         {
    5.           Sword sword = other.GetComponent<Sword>();
    6.           currentHEALTH = currentHEALTH - sword.damage;
    7.         }
    8.     }
     
    BluesyPompanno likes this.
  3. BluesyPompanno

    BluesyPompanno

    Joined:
    Jan 1, 2017
    Posts:
    37
    Thank you i didn't know you could use it this way.
     
    GamerJoHo likes this.