How to get reload function to work c# script. HELP!!

I keep running into an error that I had working. I am wondering what I did wrong. I have scanned time and time again, maybe someone knows another route or can lead me in the right direction, code below:

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

[RequireComponent(typeof(AudioSource))]
public class Pistol : MonoBehaviour
{

public Sprite idlePistol;
public Sprite shotPistol;
public float pistolDamage;
public float pistolRange;
public AudioClip shotSound;
public AudioClip reloadSound;
public AudioClip emptyGunSound;

public Text ammoText;

public int ammoAmount;
public int ammoClipSize;
int ammoLeft;
int ammoClipLeft;

bool isShot;
bool isReloading;

AudioSource source;

void Awake()
{
source = GetComponent();
ammoLeft = ammoAmount;
ammoClipLeft = ammoClipSize;
}

void Update()
{
ammoText.text = ammoClipLeft + “/” + ammoLeft;////////WhereI get error

if (Input.GetButtonDown(“Fire1”))
isShot = true;
if (Input.GetKeyDown(KeyCode.R))
{
Reload();////Get Error if I do not do below(NOTIMPLEMENTEDEXCEPTION)
}
}

private void Reload()
{
throw new NotImplementedException();
}

void FixedUpdate()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (isShot == true && ammoClipLeft > 0 && isReloading == false)
{
isShot = false;
ammoClipLeft–;
source.PlayOneShot(shotSound);
StartCoroutine(“shot”);
if(Physics.Raycast(ray, out hit, pistolRange))
{
Debug.Log(“You’ve Collided with something.” + hit.collider.gameObject.name);
hit.collider.gameObject.SendMessage(“pistolHit”, pistolDamage, SendMessageOptions.DontRequireReceiver);

}
else if (isShot == true && ammoClipLeft <= 0 && isReloading == false)
{
isShot = false;
Reload();
}
}

void Reload()
{
int bulletsToReload = ammoClipSize - ammoClipLeft;
if(ammoLeft >= bulletsToReload)
{
StartCoroutine(“ReloadWeapon”);
ammoLeft -= bulletsToReload;
ammoClipLeft = ammoClipSize;

}
else if (ammoLeft < bulletsToReload && ammoLeft > 0)
{
StartCoroutine(“ReloadWeapon”);
ammoClipLeft += ammoLeft;
ammoLeft = 0;
}
else if (ammoLeft <= 0)
{
source.PlayOneShot(emptyGunSound);
}
}

IEnumerator ReloadWeapon()

{
isReloading = true;
source.PlayOneShot(reloadSound);
yield return new WaitForSeconds(2);
isReloading = false;
}
}

IEnumerator shot()
{
GetComponent().sprite = shotPistol;
yield return new WaitForSeconds(0.1f);
GetComponent().sprite = idlePistol;
}

}

Please use code tags and post the entire error message.

One thing I can assume from this line…

ammoText.text = ammoClipLeft + "/" + ammoLeft;////////WhereI get error

…Is that you haven’t set your reference to ammoText in the inspector.

As for this line…

Reload();////Get Error if I do not do below(NOTIMPLEMENTEDEXCEPTION)

…Well, that’s just because you haven’t implemented your Reload() method yet. It is intentionally throwing this error.

private void Reload() {
   throw new NotImplementedException();
}

Edit:
Hang on, this code shouldn’t even compile. You have two of the same Reload() methods.
Are you using an IDE like Visual Studio? It would be pointing this out if so.

1 Like

Hmm it doesn’t. The Instance of Reload followed by void Reload, worked fine. Guess I tried too hard to write it in one go.

Visual Studio, yes.

  • private void Reload() {
  • throw new NotImplementedException();
  • }

This was Visual studios fix, that caused the second error for ammoText.text = ammoClipLeft + “/” + ammoLeft;

So, here would be the working script (so I thought).

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

[RequireComponent(typeof(AudioSource))]
public class Pistol : MonoBehaviour
{

public Sprite idlePistol;
public Sprite shotPistol;
public float pistolDamage;
public float pistolRange;
public AudioClip shotSound;
public AudioClip reloadSound;
public AudioClip emptyGunSound;

public Text ammoText;

public int ammoAmount;
public int ammoClipSize;
int ammoLeft;
int ammoClipLeft;

bool isShot;
bool isReloading;

AudioSource source;

void Awake()
{
source = GetComponent();
ammoLeft = ammoAmount;
ammoClipLeft = ammoClipSize;
}

void Update()
{
ammoText.text = ammoClipLeft + " / " + ammoLeft;

if (Input.GetButtonDown(“Fire1”))
isShot = true;
if (Input.GetKeyDown(KeyCode.R))
{

Reload;
}
}

void FixedUpdate()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (isShot == true && ammoClipLeft > 0 && isReloading == false)
{
isShot = false;
ammoClipLeft–;
source.PlayOneShot(shotSound);
StartCoroutine(“shot”);
if(Physics.Raycast(ray, out hit, pistolRange))
{
Debug.Log(“You’ve Collided with something.” + hit.collider.gameObject.name);
hit.collider.gameObject.SendMessage(“pistolHit”, pistolDamage, SendMessageOptions.DontRequireReceiver);

}
else if (isShot == true && ammoClipLeft <= 0 && isReloading == false)
{
isShot = false;
Reload();
}
}

void Reload()
{
int bulletsToReload = ammoClipSize - ammoClipLeft;
if(ammoLeft >= bulletsToReload)
{
StartCoroutine(“ReloadWeapon”);
ammoLeft -= bulletsToReload;
ammoClipLeft = ammoClipSize;

}
else if (ammoLeft < bulletsToReload && ammoLeft > 0)
{
StartCoroutine(“ReloadWeapon”);
ammoClipLeft += ammoLeft;
ammoLeft = 0;
}
else if (ammoLeft <= 0)
{
source.PlayOneShot(emptyGunSound);
}
}

IEnumerator ReloadWeapon()

{
isReloading = true;
source.PlayOneShot(reloadSound);
yield return new WaitForSeconds(2);
isReloading = false;
}
}

IEnumerator shot()
{
GetComponent().sprite = shotPistol;
yield return new WaitForSeconds(0.1f);
GetComponent().sprite = idlePistol;
}

}

//and yes, I get the error for the first Reload; saying it does not exist in current context. I was only confused because it worked then didnt the next day.

Use code tags please. Please go back and edit your post. Most people, including myself, won’t read the issue without them. Click on the Code: icon in the edit bar above to insert your code.

Will do next time. Problem is solved. I appreciate it.