I have a script that fires bullets and applies damage just the way I want to, but I am struggling to collect ammo and count it and run out of ammo. This is my script of what I have working so far. Please help.

using UnityEngine;
using System.Collections;

public class RaycastShoot : MonoBehaviour
{

public int gunDamage = 1;
public float fireRate = .25f;
public float weaponRange = 50f;
public float hitForce = 100f;
public Transform gunEnd;

private Camera fpsCam;
private WaitForSeconds shotDuration = new WaitForSeconds(.07f);
private AudioSource gunAudio;
private LineRenderer laserLine;
private float nextFire;

void Start ()
{
    laserLine = GetComponent<LineRenderer>();
    gunAudio = GetComponent<AudioSource>();
    fpsCam = GetComponentInParent<Camera>();
}


void Update ()
{
    if (Input.GetButtonDown ("Fire1") && Time.time > nextFire)
    {
        nextFire = Time.time + fireRate;

        StartCoroutine(ShotEffect());

        Vector3 rayOrigin = fpsCam.ViewportToWorldPoint (new Vector3(0.5f, 0.5f, 0));
        RaycastHit hit;

        laserLine.SetPosition(0, gunEnd.position);

        if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
        {
            laserLine.SetPosition(1, hit.point);

            EnemyHealth health = hit.collider.GetComponent<EnemyHealth>();

            if (health != null)
            {
                health.TakeDamage(gunDamage);
            }

            if (hit.rigidbody != null)
            {
                hit.rigidbody.AddForce(-hit.normal * hitForce);
            }
        }
        else
        {
            laserLine.SetPosition (1, rayOrigin + (fpsCam.transform.forward * weaponRange));
        }
    }
}

private IEnumerator ShotEffect ()
{
    gunAudio.Play();

    laserLine.enabled = true;
    yield return shotDuration;
    laserLine.enabled = false;
}

}

You just have a numerical value as a variable and make it so that the weapon only fires if it is greater than 0.
Than decrement that value whenever the weapon is fired.

e.g:

int ammo = 3;
if (Input.GetButtonDown("Fire1") && Time.Time < nextFire && ammo>0)
{
ammo--;
//*Your weapon firing stuff here*
}

Then to do ammo pickups you

  • Make another game object
  • Put a collider on that object
  • Check the box that says “Is Trigger”
  • Put a new script on that object call it something like “AmmoPickUp”
  • Make a public method in your RayCastShoot Class called AmmoPickUp
  • The function should take in a value called ‘amount’ and just add it to the ammo variable in that class
  • Now go back to your AmmoPickUp class and do the following :
{
 [SerializeField] int amountPickedUp = 20;
    
    void OnTriggerEnter(collider other)
    {
       RayCastShoot rcs =  GetComponent();
    
       if(rcs != null)
       {        
          rcs.AmmoPickUp(amountPickedUp);
       }
    
    }
    

[SerializeField] Makes it so that a variable is shown in the editor window without having to make it public

Stuff about OnTriggerEnter is available
from Unity - Scripting API: Collider.OnTriggerEnter(Collider)

Also, you may want to look at using tags instead of checking if a variable after using a GetComponent is null as I believe trying to get a component that doesn’t exist will throw an error anyway. In which case it would have been probably something like:

if (other.tag = "Player")
  rcs.AmmoPickUp(AmountPickedUp);

Hope this helps

@Pharez Thank you very much, I have gotten most of it working, but I’m sorry I am a little confused with these two instructions:

  • Make a public method in your
    RayCastShoot Class called AmmoPickUp

    The function should take in a value
    called ‘amount’ and just add it to
    the ammo variable in that class