Assigning a game object through script

I have a project that has multiple weapons to choose from and they all do different amounts of damage, but I’m having a problem where i have to manually choose what weapons applies to the enemies health script so when i start the game it is set on that one amount of damage no matter what weapons is being used.
Is it possible to automatically search for what weapons is being used and apply it to the enemies health script so it does the correct amount of damage.

Weapon Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponScript : MonoBehaviour
{
    public int damage;
    public float attackRate;
    public GameObject Weapon;
    public bool CanAttack = true;
    private float AttackCooldown;
    public bool IsAttacking = false;
    public bool IsEquipped = false;
    public int WeaponId;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (CanAttack)
            {
                Attack();
            }
        }

        if (Input.GetKeyDown(KeyCode.E))
            IsEquipped = true;

        if (Input.GetKeyDown(KeyCode.Q))
            IsEquipped = false;

    }

    public void Attack()
    {
        IsAttacking = true;
        CanAttack = false;
        Animator anim = Weapon.GetComponent<Animator>();
        anim.SetTrigger("Attack");
        StartCoroutine(ResetAttackCooldown());


    }

    IEnumerator ResetAttackCooldown()
    {
        StartCoroutine(ResetAttackBool());
        yield return new WaitForSeconds(AttackCooldown);
        CanAttack = true;
    }

    IEnumerator ResetAttackBool()
    {
        yield return new WaitForSeconds(1.0f);
        IsAttacking = false;

    }


}

Enemy Health Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyHealth : MonoBehaviour
{
    public WeaponScript weaponScript;
    public int enemyHealth;
    private int damageDealt;
    public int hits = 0;
    public float damageCooldown = 1.0f;
    private float attackC;
    public float AttackCooldown;

    private void Update()
    {    
        damageDealt = weaponScript.damage;
        //Debug.Log(damageDealt);

        if (enemyHealth < 1)
        {
            GetComponent<Animator>().SetTrigger("0_Health");
        }
    }

    public void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Weapon" && weaponScript.IsAttacking)
        {
            if (hits < 1)
            {
                GetComponent<Animator>().SetTrigger("Hit");
                enemyHealth -= damageDealt;
                Debug.Log("Damage: " + damageDealt);
                hits += 1;
                StartCoroutine(Damagecooldown());
               
            }
        }
    }

    IEnumerator Damagecooldown()
    {
        yield return new WaitForSeconds(damageCooldown);
        hits -= 1;
    }

}

In OnTriggerEnter(), assuming “other” is the weapon in question, and it has a WeaponScript attached: weaponScript = other.GetComponent<WeaponScript>()
and that will change it on the fly. As a matter of fact, I think you might be able to turn weaponScript into a local variable instead of a class-level variable, unless you’re using it elsewhere.

Thanks you so much that works perfectly!!!

1 Like

Glad I could help.