Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by TastyTurban · Aug 31, 2016 at 06:58 AM · noobcoroutinesparameters

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mj321 · Aug 31, 2016 at 07:02 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

https://msdn.microsoft.com/en-us/library/14akc2c7.aspx

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TastyTurban · Aug 31, 2016 at 08:44 PM 1
Share

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?

avatar image mj321 TastyTurban · Aug 31, 2016 at 09:08 PM 0
Share

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()
 {
 ...
avatar image TastyTurban mj321 · Aug 31, 2016 at 09:22 PM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

68 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges