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 RKSlither · Oct 19, 2016 at 02:37 AM · c#coroutine

Custom Coroutine With Arguments

Hey was wandering is it possible to start a coroutine which name s stored in a variable and have it also pass arguments to the coroutine? Example

 public void ExampleVoid(string coroutineName,int coroutineIntOne,int coroutineIntTwo){
 //here i want to call the coroutine by the string name which works fine
 StartCoroutine(coroutineName);
 //but what i want to do is also pass the ints to the coroutine but i have tried it a few ways and searched for help but nothing came up.
 
 //in all honesty i thought this would have been acceptable
 StartCoroutine(coroutineName(coroutineIntOne,coroutineIntTwo));
 //but it isn't acceptable and i can't seem to figure any way to do it
 }


any insight on this matter would be greatly appreciated.

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 Owen-Reynolds · Oct 19, 2016 at 03:46 AM 0
Share

The general feeling is to avoid calling a coroutine using a string. It's sort of a starter-trick. I think the idea is, once you're doing something as complicated as this, may as well call it using the real variable (which should work fine using your 2nd approach.)

avatar image b1gry4n Owen-Reynolds · Oct 19, 2016 at 03:50 AM 0
Share

ID$$anonymous$$ about the "general feeling" but as the docs say...

In most cases you want to use the StartCoroutine variation above. However StartCoroutine using a string method name allows you to use StopCoroutine with a specific method name.The downside is that the string version has a higher runtime overhead to start the coroutine and you can pass only one parameter.

avatar image Owen-Reynolds Owen-Reynolds · Oct 19, 2016 at 01:54 PM 0
Share

Right - that first sentence says to avoid the string method if you can; and the rest is obsolete. You can now store a reference to any running coroutine, which can be used to stop it. That's better since you can have 3 copies of the same coroutine running, and stop just one (stopCoroutine on a string stops them all.)

But, it takes program$$anonymous$$g experience to get used to variables that can be functions; which takes time. If, say, you're an artist with just enough scripting to get the job done, the string method is fine.

1 Reply

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

Answer by b1gry4n · Oct 19, 2016 at 03:13 AM

http://answers.unity3d.com/questions/240959/pass-more-than-one-parameter-for-startcoroutine.html

A work around in your case could be to store the variables outside the coroutine.

 int coOne;
 int coTwo;
 public void ExampleVoid(string coroutineName,int coroutineIntOne,int coroutineIntTwo){
     coOne = coroutineIntOne;
     coTwo = coroutineIntTwo;
      StartCoroutine(coroutineName);
  }

and then inside your coroutine, you could just reference "coOne" and "coTwo"

Another solution is to create a struct or class.

 public class CoVals{
     public int val1;
     public int val2;
     public CoVals(int val1, int val2){
         this.val1 = val1;
         this.val2 = val2;
     }
 }

And since only one object can be passed using the string method...

  StartCoroutine(coroutineName, new CoVals(coroutineIntOne, coroutineIntTwo));
 
 IEnumerator WhateverCoroutine(CoVals covals){
     int one = covals.val1;
     int two = covals.val2;
 }

https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html

Comment
Add comment · Show 2 · 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 flashframe · Oct 19, 2016 at 03:37 AM 0
Share

Great answer. Adding to this, you could also pass an object array:

 int intOne = 56;
 int intTwo = 10; 
 object[] parms = new object[]{intOne,intTwo};
     
 StartCoroutine("$$anonymous$$yCoroutine", parms);
 
 public IEnumerator $$anonymous$$yCoroutine(object[] parms)
 {
     int coroutineIntOne = (int)parms[0];
     int coroutineIntTwo = (int)parms[1];
     //Do something
     yield return null;
 }
avatar image RKSlither · Oct 19, 2016 at 07:49 AM 0
Share

thanks for the answer i have no idea why it never crossed my $$anonymous$$d to store it and reference it

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

7 People are following this question.

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

Related Questions

c# shooting problem (probably pretty simple) 2 Answers

StartCoroutine not listening to parameters 1 Answer

Stop previous coroutine and start new one from beginning 1 Answer

Weird problem with simple boolean and "if" statement [C#] 0 Answers

Waiting for keypress from user (how to make a co-routine?) C# 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