Accessing sword damage from another scripts ? and dealing damage.

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

public Transform Ruka;
    public int Damage;

    void Update()
    {
        transform.position = Ruka.position;
        transform.rotation = Ruka.rotation;
    }

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

public float maxHEALTH = 100;
    public float currentHEALTH;
    public int amount;

    void Start()
    {
        currentHEALTH = maxHEALTH;

    }
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
          currentHEALTH = currentHEALTH - amount;
        }
    }

    void Update()
    {
        if (currentHEALTH <= 0f)
        {
            DIE();
        }
    }
    void DIE()
    {

        Destroy(gameObject);
    }

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 ?

Hi Bluesy,

If you look at GetComponent, you should be able to use that:

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

void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
          Sword sword = other.GetComponent<Sword>();
          currentHEALTH = currentHEALTH - sword.damage;
        }
    }
1 Like

Thank you i didn’t know you could use it this way.

1 Like