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 /
avatar image
0
Question by leSamo · Dec 22, 2018 at 01:06 PM · coroutinefunctionstringcoroutinesienumerator

Make a function that can return string or IEnumerator

Hello, I'd like to make a function which can for some arguments return string and for some arguments can pause execution for some time (yield return new WaitForSeconds(1f)). For example:

 public IEnumerator Process(string s) {
     if (s == "wait") yield return new WaitForSeconds(1f);
     else return "hello";
 }
 
 Debug.Log(StartCoroutine(Process("wait"))); // pauses execution for a second
 Debug.Log(tartCoroutine(Process("hi"))); // logs "hello" to console

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Hellium · Dec 22, 2018 at 01:52 PM

This is not possible this way, a function in C# only returns one type. An alternative would be to use a Action that will be called once the coroutine ended:

 public IEnumerator Process(string input, System.Action<string> onMessageReceived )
 {
      output = null;
      if (s == "wait")
      {
           yield return new WaitForSeconds(1f);
      }
      else
      {
           output = "hello";
      }
      if( onMessageReceived != null )
           onMessageReceived( output ) ;
      yield return null ; // Needed
  }

 StartCoroutine( Process("wait", output => Debug.Log( output ) ) );
 StartCoroutine( Process("hi", output => Debug.Log( output ) ) );






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
avatar image
0

Answer by toddisarockstar · Dec 22, 2018 at 01:58 PM

your question is a bit complicated because the IEnumerator and it's wait is happening in a different thread. Since you are using a "wait", if you did successfully get your main thread to wait for a return, You pry wouldn't want to freeze your entire game while the Coroutine is processing !!!

I'm not sure what you are trying to accomplish but maybe you could change a variable in your main script instead of using a return? then you would have to check back later with your main thread to see what it changed or use a get/set trigger on the variable you changing.

i would do this:

 // wont freeze main thread    
    string xwow;
 public string wow{get{return xwow;}set{
         xwow = value;
         print (xwow);}}

 void Start () {
 StartCoroutine(Process("wait")); // pauses execution for a second
 }
 public IEnumerator Process(string s) {
     if (s == "wait") {
                     yield return new WaitForSeconds (1f);
                     wow = "ive waited";
             } else {
                     wow = "hello";}}

anyways here is a link that explains a return if you want one: link text

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 Bunny83 · Dec 22, 2018 at 02:55 PM 0
Share

No, coroutines do not run on a seperate thread. Coroutines implement cooperative multi tasking and is purely done on a single thread, the main thread. On the wiki you can find a custom CoroutineScheduler which does a similar thing as Unity's internal coroutine scheduler, but with slightly different yield values since the one implemented in Unity (WaitForSeconds, ...) store the required data internally.

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

108 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 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 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 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 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

Backspace typewriter effect 1 Answer

StopCoroutine with IEnumerator not working 2 Answers

Coroutoutine doesnt work properly. 3 Answers

Use Coroutine when object rotates 2 Answers

Collection Change During Iteration 0 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