Parameters not updating in coroutine
Hello coding elders!
I've been trying to make a small tank game, and I have made my shooting and reloading scripts as coroutines, as think this may be the most efficient method for my game.
The issue is that the parameters dont update/change when i use the parameter name in the script, but if i use the variable name directly, it works fine. IE the ammo variable wont be changed if i use the parameter. This could kind of work, but it breaks the functionality of coroutines/functions.
Here is the code I'm working on, and I've cut out most unnecessary stuff.
Edit: Thanks MJ, i dont know how the code snippet got that messed up! :l, It should be fixed now..
public class BasicShell : MonoBehaviour
{
public GameObject prefabStandardShell;
public float shotSpeed = 2000;
public float fallSpeed = 2700;
public Transform originPos;
public int fullMagAmmo = 25;
public int magAmmo = 25;
public float reloadTime = 0.5f;
public float range = 1;
public bool reloading;
// Update is called once per frame
private void Update()
{
if (magAmmo == 0)
{
StartCoroutine(ReloadWaitTime(reloadTime, magAmmo));
}
if (Input.GetKeyDown(KeyCode.Mouse0) == true && magAmmo != 0)
{
StartCoroutine(Fire(magAmmo));
}
}
public IEnumerator Fire(int ammo)
{
ammo -= 1;
GameObject shellInstance = (GameObject)Instantiate(prefabStandardShell, originPos.position, originPos.rotation);
shellInstance.GetComponent<Rigidbody>().AddForce(originPos.up * shotSpeed);
yield return new WaitForSeconds(range);
// make the shell fall after range
shellInstance.GetComponent<Rigidbody>().AddForce(originPos.forward * -fallSpeed);
}
public IEnumerator<float> ReloadWaitTime(float waitTime, int ammo)
{
yield return Timing.WaitForSeconds(waitTime);
ammo = fullMagAmmo;
// reloading = false;
print("reloaded");
yield break;
//StopCoroutine("ReloadWaitTime");
}
}
But if i do this:
public IEnumerator Fire(int ammo)
{
magAmmo -= 1;
it works fine. Sorry to post a mouthfull of code for a simple question, but i want to be clear off the bat :).
Thank you for viewing!
Please try to format the code properly. It's very hard to follow as it is. And all the "%|-1827371983_22|%" stuff probably doesn't belong there either.
Answer by mj321 · Aug 31, 2016 at 08:14 PM
public IEnumerator Fire(int ammo)
{
ammo -= 1;
You're passing magAmmo as a parameter into the Coroutine. But this will be a new variable and changing "ammo" won't affect the value of "magAmmo". Change "magAmmo" directly or pass the variable as reference.
Thank you again for your reply $$anonymous$$J!
But, when i add ref to the int ammo, i get an error saying "Iterators cannot have ref or out parameters". Is what i am trying to achieve possible in coroutines?
But, when i add ref to the int ammo, i get an error saying "Iterators cannot have ref or out parameters". Is what i am trying to achieve possible in coroutines?
Directly, yes. As a workaround you could add a new method that takes the ammo count as reference and starts the coroutine. That would also have the advantage that you don't have to write StartCoroutine() in all places where you need it.
public void Fire( ref int ammo )
{
--ammo;
StartCoroutine( DoFire() );
}
private IEnumerator DoFire()
{
...
Thank you very much mj!! It works now :)!
But i wonder why unity allows coroutines to have parameters if they dont really work though?
Regardless, thank you very much sir!
Your answer
Follow this Question
Related Questions
Trying to Invoke Void when Void has Stuff in Parameters - Easy Question 1 Answer
How to Pass a Parameter Value to an IEnumerator? 2 Answers
The Contents of my String are Lost When I Pass it to Another Method as a Parameter? 0 Answers
How to set a special prefab in a switch? 1 Answer
error the name 'lightDirty' does not exist in current context. 1 Answer