Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by Borzi · Jul 31, 2013 at 11:13 AM · javascriptfpsfunctionienumeratorreload

Java to C# conversion problem

Hey, I recently converted a script from Java to C#. I used to have a reload function which worked perfectly, but during the conversion I noticed that this function was not a void but an IEnumerator. Now the function doesn't work anymore. Anyone know why?

Script I am using (converted to C#):

 IEnumerator  Reload (){
 //Play the Reload Audio
 PlayReloadAudio();
 //Determine the seconds it takes to Reload (var ReloadTime)
 yield return new WaitForSeconds(ReloadTime);
 //Tell Unity that the player is reloading
 ReloadTimer ++;
     //Only Reload if there are Clips left
     if(Ammo > 0)
         {
         //Reset the amounts of Bullets to Default
         BulletsLeft = BulletsPerClip;
         //Subtract 1 clip
         Ammo -= BulletsFired;
         //Done Reloading
         ReloadTimer --;
         //Reset Bullets Fired
         BulletsFired = 0;
         }
 
 } 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by aldonaletto · Jul 31, 2013 at 12:20 PM

I suspect that the problem may be in the caller code: in C# you must explicitly call a coroutine with StartCoroutine, and the coroutines must explicitly return IEnumerator. In your case, Reload should be called like this:

   // start reload coroutine and return immediately:
   StartCoroutine(Reload());

If the caller code is a coroutine that chains to Reload, then the code should be changed to this:

   // start reload coroutine and wait for its completion:
   yield return StartCoroutine(Reload());

But there's another possible issue to check! If you wait the reload time by disabling shots while ReloadTimer is > 0, the order is wrong: ReloadTimer should be incremented before WaitForSeconds, so that its value would become 1 when Reload is called and return magically to 0 after the delay specified - but before the if, or else you may enter an endless loop when Reload is called and there are no more clips:

 IEnumerator  Reload (){
   //Play the Reload Audio
   PlayReloadAudio();
   //Tell Unity that the player is reloading
   ReloadTimer ++;
   //Determine the seconds it takes to Reload (var ReloadTime)
   yield return new WaitForSeconds(ReloadTime);
   //Delay is over
   ReloadTimer --;
   //Only actually Reload if there are Clips left
   if(Ammo > 0){
     //Reset the amounts of Bullets to Default
     BulletsLeft = BulletsPerClip;
     //Subtract 1 clip
     Ammo -= BulletsFired;
     //Reset Bullets Fired
     BulletsFired = 0;
   }
 }



Comment
Add comment · Show 6 · 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 Bunny83 · Jul 31, 2013 at 12:25 PM 0
Share

Hmm, maybe he's missing the actual class around his function?

avatar image aldonaletto · Jul 31, 2013 at 01:25 PM 0
Share

Do you mean the script class? This would produce lots of compilation errors, and it seems that the code just doesn't do what the OP expects - but that's a valid point to check.

avatar image Borzi · Jul 31, 2013 at 04:40 PM 0
Share

No I am not getting any compilation errors. Is this how you mean it:

 if(BulletsLeft == 0)
     {
     //Automatic Reload if the Player is out of Ammo
     StartCoroutine(Reload());
     }
avatar image Borzi · Jul 31, 2013 at 04:50 PM 0
Share

Update: Yep it did! Thank you :)

avatar image aldonaletto · Aug 02, 2013 at 03:59 AM 1
Share

One more thing: you should start Reload only once in order to avoid multiple Reload coroutines running in parallel. Apparently you could do it with a $$anonymous$$or change in your code:

 // don't call Reload if another Reload is running:
 if (BulletsLeft == 0 && ReloadTimer == 0){
     StartCoroutine(Reload());
 }
Show more comments

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

18 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

Related Questions

Some issues with weapon switching and reloading? 2 Answers

UnityEngine has no appropriate version problem 1 Answer

Problem accessing variable in javascript 1 Answer

How To Make Ammo & Realod for Gun & Spark for Gun ? 0 Answers

Accessing function from another script won't work 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