Search Unity

Question Problem with gun damage and enemy health

Discussion in 'Scripting' started by BiscuitBoys, Mar 1, 2021.

  1. BiscuitBoys

    BiscuitBoys

    Joined:
    Mar 1, 2021
    Posts:
    4
    Hello, I am working on a FPS game and right now I’m stuck with this 1 problem. I've got a script called EnemyHealth in which there's a function called "DealDamage" it looks something like this:


    Code (CSharp):
    1. public void DealDamage(int damage)
    2.  
    3.     {
    4.  
    5. currentHealth -= damage;
    6.  
    7.  
    8.         if (currentHealth <= 0 && damage >= 15)
    9.  
    10.         {
    11.  
    12.             while (fleshAmount > 0)
    13.  
    14.             {
    15.  
    16.                 fleshAmount--;
    17.  
    18.                 GameObject flesh= Instantiate(fleshPrefab, transform.position, Quaternion.identity);
    19.  
    20.  
    21.                 Destroy(flesh, 12);
    22.  
    23.             }
    24.  
    25.         }
    26.  
    27.  
    28.         CheckIfDead();
    29.  
    30.     }

    Whenever I shoot an enemy when its health is less than 0 and the gun deals more than 15 damage the enemy dies and it spawns the correct gameobjects. The problem is when I shoot an enemy with a shotgun it does not spawn any gameobjects because my shotgun script fires multiple rays which all deal less than 15 damage.

    I was thinking of creating maybe some kind of a timer to check when was the last time the enemy got shot, but I don't think it's the right way to do it.


    All help is appreciated, thank you in advance.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    What is the purpose of the check for at least 15 damage? It sounds like it makes weak damage pointless.

    You could consider adding up all the damage applied by each ray from the shotgun, and applying it all then once. I'm guessing the shotgun is then likely to deal more than 15 damage, and the player is probably expecting it to work more like this anyway.
     
  3. supersonicglitchy4

    supersonicglitchy4

    Joined:
    Aug 22, 2021
    Posts:
    2
    ok so im having this problem with the code, MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
    its a unity 3d fps game and here is the gun code that i did which is probably causing the problem:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class gun : MonoBehaviour
    4. {
    5.     public float damage = 10f;
    6.     public float range = 100f;
    7.     public float impactForce = 30f;
    8.  
    9.     public Camera fpsCam;
    10.     public ParticleSystem muzzleFlash;
    11.     public GameObject impactEffect;
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.  
    17.         if (Input.GetButtonDown("Fire1"))
    18.         {
    19.             Shoot();
    20.         }
    21.  
    22.     }
    23.  
    24.     void Shoot()
    25.     {
    26.         muzzleFlash.Play();
    27.  
    28.         RaycastHit hit;
    29.         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
    30.         {
    31.             Debug.Log(hit.transform.name);
    32.  
    33.             Target target = hit.transform.GetComponent<Target>();
    34.             if (target != null)
    35.             {
    36.                 target.TakeDamage(damage);
    37.             }
    38.  
    39.             if (hit.rigidbody != null)
    40.             {
    41.                 hit.rigidbody.AddForce(-hit.normal * impactForce);
    42.             }
    43.  
    44.             GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
    45.             Destroy(impactGO, 2f);
    46.         }
    47.     }
    48. }
    yes i did use brackey's tutorial
    and i still get the error.
    how can i fix this?[/code]
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Everything you posted above is irrelevant. NullReference / MissingReferences NEVER require a post to any form, let alone a necro-post to a six-month old topic.

    The reason this is true is because the answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

    - drag it in using the inspector
    - code inside this script initializes it
    - some OTHER external code initializes it
    - ? something else?

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032