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
0
Question by ExpiredIndexCard · Jun 14, 2013 at 11:13 PM · coroutineyieldreloadwait

C# yield not working.

I am making a reload time, but when I start a coroutine, it doesn't wait for 3 seconds and then reloads. Here is my code:

void Update(){

 if(ammoInClip == 0){
         StartCoroutine("reload", 3f);
         ammoInClip = 30;
     }

}

 IEnumerator reload(float wait){
         yield return new WaitForSeconds (wait);
     }
Comment
Add comment · Show 3
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 InfiniBuzz · Jun 14, 2013 at 11:27 PM 0
Share

Hi

Your coroutine waits 3 seconds and nothing more (in a coroutine). So you need to put the ammoInClip inside the coroutine or better delete the coroutine and make the yield before the ammoInClip = 30 line.

 if (ammoInClip == 0)
 {
    yield return new WaitForSeconds(3);
    ammoInClip = 30;
 }

Hope it helps

avatar image OP_toss · Jun 14, 2013 at 11:28 PM 1
Share

I think you don't quite know how Coroutines work...

You need to reload after yielding. The StartCoroutine method doesn't wait, it just starts up a coroutine that waits. But as you have it, the coroutine starts, waits for 3 seconds, and doesn't do anything.

 IEnumerator Reload( float wait)
 {
   yield return new WaitForSeconds( wait );
   ammoInClip = 30;
 }

Now the coroutine waits, then reloads. Now you need to change the if statement in your Update loop so you aren't firing off tons of coroutines every update for 3 seconds. Just have a isReloading bool that you flip on when you fire and check against before firing.

$$anonymous$$ake sense? Hope this helps!

avatar image bothelho · Apr 16, 2014 at 03:44 AM 0
Share

thank you to Op_toss, your answer help me out of this!

i have the wrong concept about the "yield wait", you just correct me, thanks!

2 Replies

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

Answer by roberto_sc · Jun 15, 2013 at 12:18 AM

The problem happens because when you yield, you immediately return the control to the Update method. It then executes the ammoInClip = 30. After 3 seconds, the control returns to Reload - that does nothing.

That's why you have to put ammoInClip = 30 after the yield, so it can return the control to the Update method, continue the game and come back after 3 seconds to reload.

 void Update(){
     if(ammoInClip == 0){
        StartCoroutine(Reload(3f));
     }
 }
 
 IEnumerator Reload(float wait){
    yield return new WaitForSeconds (wait);
    ammoInClip = 30;
 }

Note the call for StartCoroutine, I'm not using a string, better for type-safety.

Do you come from Java? Methods in C# usually starts with lowercase :)

Comment
Add comment · Show 1 · 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 ExpiredIndexCard · Jun 15, 2013 at 02:41 AM 0
Share

Thanks so much for your help. I just found out about coroutines today lol. Thanks

avatar image
0

Answer by umangindianic · Jul 02, 2013 at 06:59 AM

With the use of code and whenever you want to StartCoroutine that has require the method name and its argument if you have passed.

 void Update(){
     if(ammoInClip == 0){
       StartCoroutine(reload(3.0f));
       ammoInClip = 30;
     }
 
 }
 
 IEnumerator reload(float wait){
    yield return new WaitForSeconds (wait);
 }
Comment
Add comment · 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

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

20 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

Related Questions

Alternative for semaphores in Unity? 2 Answers

WaitUntil Combined With sqrMagnitude is not working? 1 Answer

C# Coroutine help 1 Answer

Wierd issue with Coroutines? 2 Answers

WaitForSeconds Question 2 Answers


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