• 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 name123 · Aug 17, 2012 at 06:37 PM · c#.net

How to store coroutines inside a List?

I want to create a List of coroutines to start if future. How to store such functions inside a list?

Say we have a function like:

 IEnumerator Example() {
     while (true) {
         yield return  new WaitForFixedUpdate();
     }
 }

We cant store it inside something like `List> list` but we get compiler error like:

 Error    1    The best overloaded method match for 'System.Collections.Generic.List<System.Func<System.Collections.IEnumerator>>.Add(System.Func<System.Collections.IEnumerator>)' has some invalid arguments

when we call

            list.Add(Example);

or if we use `List list` we get no compile errors but on

             if (disposeActions.Any()) {
                 yield return StartCoroutine(list[0]); //here we get null exception
                 disposeActions.RemoveAt(0);
             }

So I wonder how to create a list to store IEnumerator functions and be capable to send its items to coutines?

Comment
Add comment · Show 7
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 Khada · Aug 17, 2012 at 07:03 PM 0
Share

Read up on delegates: http://msdn.microsoft.com/en-us/library/ms173171(v=vs.80).aspx

avatar image Kryptos · Aug 17, 2012 at 07:32 PM 1
Share

@khada I don't see the point.

avatar image Khada · Aug 17, 2012 at 07:38 PM 0
Share

You can use delegates to hold a reference to a method. That method can contain your coroutine code and the delegates can go into a list. *Haven't done exactly this before but makes sense to me at a glance at least.

avatar image Kryptos · Aug 18, 2012 at 08:47 AM 0
Share

Coroutines are not exaclty methods since the compiler generate an anonymous class, so I'm not sure you can call them using delegates.

You could create a method corresponding to each coroutine that call StartCoroutine. But that sounds a bit heavy.

avatar image name123 · Aug 18, 2012 at 10:59 AM 0
Share

@$$anonymous$$ryptos: the point is to `yield return StartCorutine(function)` s from that list one by one (so to say destroy only one asset bundle at a time)

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Paulo-Henrique025 · Aug 17, 2012 at 07:37 PM

Store strings!

 public List<String> coroutinesList = new List<String>();
 
 void CallStoredRoutine(int index)
 {
     StartCoroutine(coroutineList[index]);
 }
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 Khada · Aug 17, 2012 at 07:40 PM 0
Share

Wow, nice to know.

avatar image
0

Answer by MaDDoX · Jan 19, 2017 at 04:44 AM

Did you try this?

 List<Func<IEnumerator>> coroutines;

When iterating, make sure to check the element for null before invoking the coroutine.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

how can i use ZipArchive class ,How can i use .net ZipArchive Class 0 Answers

Print JSON to screen as a block of text in unity during runtime 1 Answer

Are switch -expressions- compatible with Unity? 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