- Home /
C# syntax seems fine, why wont yeild work? (Unsolved)
Hello, I am working on the beginnings of an ammo script. I am using an if statement to check if the ammo in the clip is < 1. When that happens I StartCoroutine. In this IEnumerator, I reload clip, subtract from total ammo and yield for seconds. However, the yield never seems to work.
It seems as though it returns right back to the update() once those variables are calculated but never waitsforseconds.
//Here are parts of my code
public class PistolScript : MonoBehaviour {
public PlayerInventory inventoryScript;
public float maxClipAmount;
public float ammoInClip;
public float currentAmmo;
public float reloadTime;
void FixedUpdate () {
if(ammoInClip < 1){
StartCoroutine(LoadGun ());
}
}
IEnumerator LoadGun(){
ammoInClip = maxClipAmount;
currentAmmo = currentAmmo - ammoInClip;
yield return new WaitForSeconds(2);
}
//I've also arranged the IEnumerator differently, if that does anything at all.
IEnumerator LoadGun(){
yield return new WaitForSeconds(2);
ammoInClip = maxClipAmount;
currentAmmo = currentAmmo - ammoInClip;
}
I also want to replace the waitforseconds float with the float reloadTime. I assume it will work when I find out this problem.
Thankyou for any help pointing out my flaw.
Answer by Cherno · Aug 20, 2014 at 07:18 PM
Try using a seperate bool set to true while reloading:
private bool reloading = false;
void FixedUpdate () {
if(ammoInClip < 1){
if(reloading == false) {
StartCoroutine(LoadGun ());
}
}
}
IEnumerator LoadGun(){
reloading = true;
ammoInClip = maxClipAmount;
currentAmmo = currentAmmo - ammoInClip;
yield return new WaitForSeconds(2);
reloading = false;
}
Thanks for your response, I now tried what you just provided. And still once it reaches the yield, it does not continue to the next line of code so I stay in reloading.
It should not do that I know, because if have another script that uses the same technique with a bool and its bool changes back after the yield.
//Here is the entire script incase there is anything else that might mess with it.
using UnityEngine;
using System.Collections;
public class PistolScript : $$anonymous$$onoBehaviour {
public PlayerInventory inventoryScript;
public Player$$anonymous$$ovement movementScript;
public GameObject leftBullet;
public GameObject rightBullet;
public bool facingRight;
public GameObject spawnObj;
Vector2 spawnpoint;
public float maxClipAmount;
public float ammoInClip;
public float currentAmmo;
public float reloadTime;
public bool reloading = false;
void Start () {
currentAmmo = inventoryScript.currentNormalPistolAmmo;
ammoInClip = maxClipAmount;
}
void Update(){
}
IEnumerator LoadGun(){
reloading = true;
ammoInClip = maxClipAmount;
currentAmmo = currentAmmo - ammoInClip;
yield return new WaitForSeconds(2);
reloading = false;
}
void FixedUpdate () {
facingRight = movementScript.facingRight;
spawnpoint = spawnObj.transform.position;
if(ammoInClip < 1){
if(!reloading){
StartCoroutine(LoadGun ());
}
}
if (Input.GetButtonDown("Shoot") && facingRight && ammoInClip > 0 && !reloading) {
GameObject clone;
clone = Instantiate(rightBullet, spawnpoint, transform.rotation) as GameObject;
--ammoInClip;
}
if (Input.GetButtonDown("Shoot") && !facingRight && ammoInClip > 0 && !reloading) {
GameObject clone;
clone = Instantiate(leftBullet, spawnpoint, transform.rotation) as GameObject;
--ammoInClip;
}
}
}