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 Le-Capitaine · Sep 04, 2014 at 07:12 PM · c#yieldwaitforsecondscoroutines

Mysteries of yield

 public GameObject Pool (GameObject thing, float delay) {
     StartCoroutine(PoolDelayed(thing, delay, 0));
     return thing;
 }
 
 IEnumerator PoolDelayed (GameObject thing, float delay, int dummy) {
     Debug.Log ("PoolDelayed is called.");
     yield return new WaitForSeconds (delay);
     Pool (thing);
 }

Debug.Log (as well as the actual game) reveals that instead of waiting for delay then proceeding, the whole coroutine is repeated every frame for the whole duration of delay.

I've actually heard of some amount of wonkiness regarding everything around yield, but this is the first time I ever have WaitForSeconds() do that in all the times I've been using it. Has somebody got some context to it and/or maybe a solution?

Comment
Add comment · Show 2
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 Landern · Sep 04, 2014 at 07:20 PM 0
Share

Where is the rest of the implementation that calls Pool originally?

And what does the other method called Pool that only takes a GameObject and no float do?

avatar image Le-Capitaine · Sep 04, 2014 at 07:30 PM 0
Share

The other Pool() is the actual pooling function:

     public GameObject Pool (GameObject thing) {
         if (thing.tag == "AI") {
             AI script = thing.GetComponent<AI>();
             AIPools [script.poolNumber].pool.Add (thing);
         }
         else if (thing.tag == "Projectile") {
             Projectile script = thing.GetComponent<Projectile>();
             bulletPools [script.poolNumber].pool.Add (thing);
         }
         Disappear (thing);
         thing.transform.parent = poolParent.transform;
         thing.SetActive (false);
         return thing;
     }

And here's one of the two callers, itself called only once:

     void Remove () {
         battle.Disappear (gameObject);
         battle.Pool (gameObject, effects[0].lifetime);
         if (parent != null) {
             parent.subProjectiles.Remove (this);
             if (parent.subProjectiles.Count <= 0) battle.Pool (parent.gameObject, parent.effects[0].lifetime);
         }
     }

It's for an object pooling system.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kiwasi · Sep 04, 2014 at 07:20 PM

You've got some weird logic happening here. You are also missing code relevant to the problem here.

The main question is where you are starting the sequence from. If you are calling either Pool () or StartCoroutine (PoolDelay()) every frame from Update you would see the behaviour you are experiencing.

This is not the normal way to code a repetitive loop in a coroutine, the normal way would be to use a while loop.

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 Le-Capitaine · Sep 04, 2014 at 07:33 PM 0
Share

I only have it called occasionally upon removal (or "removal") of an object, as shows the code I've posted up above. $$anonymous$$ore than that, just now I tried to have Pool() skipping PoolDelayed() altogether straight to the other Pool() and no issue popped up at all.

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

24 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

Related Questions

Yield/OnTriggerEnter Help C# 1 Answer

C# WaitForSeconds doesn't seem to work ?? 2 Answers

(c#) Problems with yield 1 Answer

Playing footsteps with interval 1 Answer

Run Coroutine once, but finish Lerping 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