- Home /
Reload function in my script not working at all. No error messages in compiler.
using UnityEngine;
using System.Collections;
public class shootScript : MonoBehaviour {
public Rigidbody bullet;
private int throwPower = 200;
public float fireRate = 0.15f;
private float nextFire = 0.0f;
public float soundRate = 0.15f;
private float nextSound = 0.0f;
private float flashRate = 0.15f;
private float nextFlash = 0.0f;
public int clips = 10;
public int maxClip = 60;
public int ammoClip;
private int bulletsReq;
private float reloadTime = 1.6f;
public int totalBullets = 600;
public bool canFire = false;
public Animation gunRecoil;
public GameObject akShot;
public GameObject finalShot;
public GameObject muzzleBlast;
// Use this for initialization
void Update () {
bullet = bullet.GetComponent<Rigidbody> ();
if (ammoClip > 0)
canFire = true;
if (ammoClip <= 0)
canFire = false;
if (ammoClip > maxClip)
ammoClip = maxClip;
if (Input.GetButton ("Fire1") && canFire == true)
IsFiring ();
if (Input.GetButtonDown ("Fire1") && canFire == true)
IsFiring ();
if (Input.GetButtonDown ("Fire2") && ammoClip < maxClip)
Reload();
canFire = false;
}
// Fires one shot each time the button is pressed.
void IsFiring () {
if (ammoClip > 0 && canFire == true) {
GameObject muzzleFlash = Instantiate (muzzleBlast, this.transform.position, this.transform.rotation) as GameObject;
GameObject lastShot = Instantiate (finalShot, this.transform.position, this.transform.rotation) as GameObject;
Rigidbody clone = Instantiate (bullet, transform.position, transform.rotation) as Rigidbody;
clone.velocity = transform.TransformDirection (Vector3.forward * throwPower);
ammoClip --;
}
else if (ammoClip > 0 && canFire == true && Time.time > nextFire && Time.time > nextSound && Time.time > nextFlash) {
nextFire = Time.time + fireRate;
nextSound = Time.time + soundRate;
nextFlash = Time.time + flashRate;
Rigidbody clone = Instantiate (bullet, transform.position, transform.rotation) as Rigidbody;
clone.velocity = transform.TransformDirection (Vector3.forward * throwPower);
GameObject gunShot = Instantiate (akShot, this.transform.position, this.transform.rotation) as GameObject;
GameObject muzzleFlash = Instantiate (muzzleBlast, this.transform.position, this.transform.rotation) as GameObject;
ammoClip --;
if (ammoClip == 0)
canFire = false;
StartCoroutine (Reload ());
}
}
IEnumerator Reload () {
if (ammoClip == 0)
canFire = false;
if (Input.GetKeyDown (KeyCode.R) && ammoClip > 0)
canFire = false;
yield return new WaitForSeconds (reloadTime);
ammoClip = maxClip;
}
}
try switching line 75 and 74 around, that might help. load the co-routine before the you switch the bool. not sure if that will help but worth a shot
Answer by ndrummer31 · Apr 16, 2015 at 05:23 AM
Hi FutureTech_Coker! There is no compiler error because technically there is no error. The "error" is very simple, and I noticed you did more than once in your code. If statements only work without curly brackets if there is ONE line of code within it.
What you wrote was:
if (Input.GetButtonDown ("Fire2") && ammoClip < maxClip)
Reload();
canFire = false;
So the compiler is really reading this:
if (Input.GetButtonDown ("Fire2") && ammoClip < maxClip)
{
Reload();
}
canFire = false;
It should be:
if (Input.GetButtonDown ("Fire2") && ammoClip < maxClip)
{
Reload();
canFire = false;
}
Another instance with the same error:
if (ammoClip == 0)
canFire = false;
StartCoroutine (Reload ());
I hope this helps and as always, happy coding :)
~nDrummR
I thought that was gonna work drummer but for some reason it's still doing the same thing. Letting me play and fire but it won't let me reload.
Answer by revolute · Apr 17, 2015 at 01:13 AM
Well, your branching is wrong.
Look at this :
if (ammoClip > 0 && canFire == true){}
else if (ammoClip > 0 && canFire == true && Time.time > nextFire && Time.time > nextSound && Time.time > nextFlash){}
You will never get into else if with this check.
If(ammoClip > 0 && canFire == true) is true, then 'if' is entered and not 'else if'. If(ammoClip > 0 && canFire == true) is false, then 'if' wont be entered but neither will it enter 'else if'.
Fix this part and it will work.
And FYI, IEnumerators only work when you call it with StartCoroutine. Sadly, your Reload() in line 46 will never be run, which may also be a part of your troubles.
So I would have to put StartCoroutine next to reload in the update function as well?
Anyways, whenever you call a IEnumerator method, remember to use StartCoroutine as calling the method without it wont get it run.
And compiler wont throw any errors or warnings.
Thanks everyone you guys have all been pretty helpful, I finally got it working. Thanks Again.....
using UnityEngine; using System.Collections;
public class shootScript : $$anonymous$$onoBehaviour {
public Rigidbody bullet;
private int throwPower = 200;
public float fireRate = 0.15f;
private float nextFire = 0.0f;
public float soundRate = 0.15f;
private float nextSound = 0.0f;
private float flashRate = 0.15f;
private float nextFlash = 0.0f;
public int clips = 10;
public int maxClip = 60;
public int ammoClip;
private int bulletsReq;
private float reloadTime = 1.6f;
public int totalBullets = 600;
public bool canFire = false;
public Animation gunRecoil;
public GameObject akShot;
public GameObject finalShot;
public GameObject muzzleBlast;
// Use this for initialization
void Update () {
bullet = bullet.GetComponent<Rigidbody> ();
if (ammoClip > 0)
canFire = true;
if (ammoClip <= 0)
canFire = false;
if (ammoClip > maxClip)
ammoClip = maxClip;
if (Input.GetButton ("Fire1") && canFire == true)
IsFiring ();
if (Input.GetButtonDown ("Fire1") && canFire == true)
IsFiring ();
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.R) && canFire < maxClip)
{
StartCoroutine (Reload());
canFire = false;
}
}
// Fires one shot each time the button is pressed.
void IsFiring () {
if (ammoClip > 0 && canFire == true) {
GameObject muzzleFlash = Instantiate (muzzleBlast, this.transform.position, this.transform.rotation) as GameObject;
GameObject lastShot = Instantiate (finalShot, this.transform.position, this.transform.rotation) as GameObject;
Rigidbody clone = Instantiate (bullet, transform.position, transform.rotation) as Rigidbody;
clone.velocity = transform.TransformDirection (Vector3.forward * throwPower);
ammoClip --;
}
else if (ammoClip > 0 && canFire == true && Time.time > nextFire && Time.time > nextSound && Time.time > nextFlash) {
nextFire = Time.time + fireRate;
nextSound = Time.time + soundRate;
nextFlash = Time.time + flashRate;
Rigidbody clone = Instantiate (bullet, transform.position, transform.rotation) as Rigidbody;
clone.velocity = transform.TransformDirection (Vector3.forward * throwPower);
GameObject gunShot = Instantiate (akShot, this.transform.position, this.transform.rotation) as GameObject;
GameObject muzzleFlash = Instantiate (muzzleBlast, this.transform.position, this.transform.rotation) as GameObject;
ammoClip --;
if (ammoClip == 0) {
Reload ();
canFire = false;
}
}
}
IEnumerator Reload () {
if (ammoClip == 0)
canFire = false;
else if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.R) && ammoClip > 0) {
yield return new WaitForSeconds (reloadTime);
canFire = false;
ammoClip += maxClip;
}
}
}
Answer by lordlycastle · Apr 16, 2015 at 03:56 AM
Well of course it’s working, otherwise it’ll give compiler error. Try using the debugger, it’ll help you understand what’s happening. Put a breakpoint in Reload and see how it executes line by line. If you have Monodevelop here are the instructions. I’m sure you’ll find them for VS, just google it. Otherwise you can just use Debug.Log
, the functionality is limited with it.
Answer by FutureTech_Coker · Apr 17, 2015 at 01:01 AM
Well I was certain your answer was going to work drummer, but for some silly reason it's still doing the exact same thing. It's letting me play and fire but it will not let me reload at all. I am lost on it. I want to be able to reload the difference needed to fill the clip when I hit R or fire2 ATM, when I have more then 0 ammo in my current ammoClip.
Your answer
![](https://koobas.hobune.stream/wayback/20220613193235im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do I make my bullet tracers show when shooting in the air? 0 Answers
JS and C# how to open in different programs. 1 Answer
Overwrite the value in my List but why? 0 Answers