Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

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

Discussion in 'Getting Started' started by createappeal, Jul 25, 2019.

  1. createappeal

    createappeal

    Joined:
    Jul 10, 2019
    Posts:
    13
    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<AudioSource>();
    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<SpriteRenderer>().sprite = shotPistol;
    yield return new WaitForSeconds(0.1f);
    GetComponent<SpriteRenderer>().sprite = idlePistol;
    }

    }
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Please use code tags and post the entire error message.

    One thing I can assume from this line...
    Code (CSharp):
    1. ammoText.text = ammoClipLeft + "/" + ammoLeft;////////WhereI get error
    ...Is that you haven't set your reference to
    ammoText
    in the inspector.

    As for this line...
    Code (CSharp):
    1. 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.
    Code (CSharp):
    1. private void Reload() {
    2.    throw new NotImplementedException();
    3. }
    4.  
    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.
     
    Last edited: Jul 25, 2019
    createappeal likes this.
  3. createappeal

    createappeal

    Joined:
    Jul 10, 2019
    Posts:
    13
    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.
     
  4. createappeal

    createappeal

    Joined:
    Jul 10, 2019
    Posts:
    13
    Visual Studio, yes.
     
  5. createappeal

    createappeal

    Joined:
    Jul 10, 2019
    Posts:
    13
    1. private void Reload() {
    2. throw new NotImplementedException();
    3. }
    This was Visual studios fix, that caused the second error for ammoText.text = ammoClipLeft + "/" + ammoLeft;
     
  6. createappeal

    createappeal

    Joined:
    Jul 10, 2019
    Posts:
    13
    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<AudioSource>();
    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<SpriteRenderer>().sprite = shotPistol;
    yield return new WaitForSeconds(0.1f);
    GetComponent<SpriteRenderer>().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.
     
  7. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    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.
     
  8. createappeal

    createappeal

    Joined:
    Jul 10, 2019
    Posts:
    13
    Will do next time. Problem is solved. I appreciate it.