What is the best way to go about having enemies take adjusted damage from cloned bullets?

Hello everyone! This is my first time posting here, so I apologize for any errors in formatting or the like. Long story short, I am creating my first 3d third person shooter, and I’ve come across a major roadblock. I’ve gotten my gun to shoot, and the enemies used to take damage. The problem occurs when I try to modify the damage of the bullets - in this case, through in ability. I’ve learned that the way the enemies took damage before hand was because the enemies health was referencing the prefab script, and any attempt to have the enemies reference the cloned bullets instead has ended in failure.

I’m using 4 scripts, Rally (The ability), FireGun (Which handles spawning the bullets), Bullet (The code on the prefab that handles the bullet damage and destruction), and EnemyHealth (Which handles the Enemy health and destroys the enemy when its health reaches zero)

Here are the scripts:

Rally

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

public class Rally : MonoBehaviour
{
 

    public float RallyTime =1f;

   

    public bool RallyIsActive = false;

    private void Start()
    {
       
    }

    public void Update()
    {
        if (Input.GetKeyDown(KeyCode.F))
        {

           
            StartCoroutine(RallyDuration());
           


        }
    }

    IEnumerator RallyDuration()
    {
        yield return new WaitForSeconds(RallyTime);
        RallyIsActive = true;
       
   
    }
}

FireGun

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class FireGun : MonoBehaviour
{
    public GameObject bullet;
    public Transform bulletspawn;
    private float bulletspeed = 50;
    public float FireRate = 4f;
    public float ReloadTime = 2f;
    public int TotalAmmo = 45;
    public int CurrentAmmo;
    public bool Fired = false;


    public GameObject BulletClone;
    public Bullet BulletCloneScript;
    [Space]
   

   
  

    public Rally rally;

   

   

   

    public Text AmmoCount;
   
    void Start()
    {


        BulletClone = Instantiate(bullet, bulletspawn.position, bulletspawn.rotation);

        CurrentAmmo = TotalAmmo;
      
    }

  
    void Update()
    {
        if (Input.GetMouseButtonDown(0) && CurrentAmmo > 0)
        {
            Fire();
           
           
           
           
        }
        else if (Input.GetMouseButton(0) && CurrentAmmo <= 0)
        {
           
            Debug.Log("Out of ammo");
          
        }

        if (Input.GetKeyDown(KeyCode.R))
        {
            Invoke("Reload", 0.5f);
        }

      


      

    }

    public void Fire()
    {

       BulletClone = Instantiate(bullet, bulletspawn.position, bulletspawn.rotation);
        BulletClone.GetComponent<Rigidbody>().AddForce(bulletspawn.forward * bulletspeed, ForceMode.Impulse);
        CurrentAmmo -= 1;
        Fired = true;
        if (rally.RallyIsActive)
        {
            BulletClone.GetComponent<Bullet>().NormalDamage = 300;
            Debug.Log("Fired" + BulletClone + "with" + BulletClone.GetComponent<Bullet>().NormalDamage + "damage!");
            BulletCloneScript = BulletClone.GetComponent<Bullet>();
           

        }

        Debug.Log("Fired"  + BulletClone);



    }

  // Handles reloading the gun

    public void Reload()
    {
       
        if (CurrentAmmo < TotalAmmo)
        {
            CurrentAmmo += TotalAmmo - CurrentAmmo ;

            Debug.Log("Reloading");

        }

        if (CurrentAmmo == TotalAmmo)
        {

            Debug.Log("Ammo Full.");
        }
    }




  
}

Bullet

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

public class Bullet : MonoBehaviour
{

    private float DestroyTime = 0.5f;

    public float NormalDamage;
    public float BoostedDamage;

    public bool IncreasedDamage = false;





    private void Awake()
    {
        NormalDamage = 20f;
    }

    // Start is called before the first frame update
    void Start()
    {

       
        NormalDamage = BoostedDamage;

}

    // Update is called once per frame
    public void Update()
    {
      
        Invoke("DestroyBullet", DestroyTime);

      
    }

    public void DestroyBullet()
    {
        Destroy(gameObject, DestroyTime);
    }

    public void RallyEffect()
    {
        IncreasedDamage = true;
        NormalDamage = 300;

    }
}

EnemyHealth

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

public class EnemyHealth : MonoBehaviour

{
    public float StartingEnemyhealth = 200;
    public float Enemyhealth;

    public GameObject Enemycharacter;
   
   
    public Grenade grenadedamage;
    public Rally rallyref;
    public FireGun Firegunref;

    public Image healthbar;

   


    // Start is called before the first frame update
    void Start()
    {
        Enemyhealth = StartingEnemyhealth;

      


    }

    // Update is called once per frame
    public void Update()
    {


        healthbar.fillAmount = Enemyhealth / StartingEnemyhealth;

        if (Enemyhealth <= 0)
        {
            Destroy(gameObject);
        }




    }



    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "bullet")
        {


            Enemyhealth -= Firegunref.BulletCloneScript.NormalDamage;
            Update();
            Debug.Log("Enemy has taken" + Firegunref.BulletClone.GetComponent<Bullet>().NormalDamage + "damage");


        }

       


    }

    public void GrenadeDamage()
    {
        Enemyhealth -= 150;
    }

 

   
}

Any help would be much appreciated, as I’ve been scratching my head over this for days!

Maybe just do like:

Enemyhealth -= collision.gameObject.GetComponent<Bullet>().NormalDamage;

Also it’s a bad idea calling Update(); manually in your collision script. Unity does that automagically.

1 Like

Thank you! That was the little thing I was missing that I couldn’t wrap my head around. I’ll remove the manual Update() as well - I’m a pretty new coder, and that was the product of hours of frustration.

1 Like

No problemo