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 catwoman · Mar 23, 2013 at 11:18 AM · coroutinewww

Coroutines and Static function

Hi, I need to call a coroutine from a static function.

I don't know too much about coroutines... but I think I need it for loading a file using WWW.

I found this, but I don't understand how to use it...

Here is my static function:

 public static string getFile(string path){
 
 www = new WWW(path);
 
 return www.text;
 
 }
 

Any helps?

Comment
Add comment · Show 1
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 drudiverse · Nov 21, 2015 at 08:29 AM 0
Share

Seriously?

To have a web page processing in another script is that complicated?

I am having the same: Coroutine 'imgcube' cannot be automatically started from a static function.

and i have to spend 1 hour to learn this new type of event handling ins$$anonymous$$d of having a yield statement in a static function?

that's well inconvenient for U3D 1/10 for workflow!

1 Reply

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

Answer by whydoidoit · Mar 23, 2013 at 11:40 AM

Ok so you can't do what you want to do quite like that. The WWW will not wait when you call this, so when you call getFile you cannot have the string immediately. A good way of handling this is to use closures (it's techy, more on Unity Gems).

So I presume you currently do something like this:

   someString = SomeClass.getFile("SomeURL");
   Debug.Log(someString);
   Process(someString);

Because of the delay we need to execute the 2nd and 3rd lines when the data is available, not straight away. To do that we create an Action closure.

   SomeClass.getFile("SomeURL",(someString)=>{
      Debug.Log(someString);
      Process(someString);
   });

So we've defined the subsequent statements as a thing we can call when the data is available. Now we need to modify the code from that other thread so that it can deal with all of this! Here's the code - add it to an new empty GameObject in your scene:

StaticCoroutine.cs

 public class StaticCoroutine : MonoBehaviour{
 
  static public StaticCoroutine instance; //the instance of our class that will do the work
 
  
 
  void Awake(){ //called when an instance awakes in the game
 
   instance = this; //set our static reference to our newly initialized instance
 
  }

  IEnumerator Perform(IEnumerator coroutine, Action onComplete = null)
  {
      onComplete = onComplete ?? delegate {};
      yield return StartCoroutine(coroutine);
      onComplete();
  }
 
  static public void DoCoroutine(IEnumerator coroutine, Action onComplete = null){
 
   instance.StartCoroutine(instance.Perform(coroutine, onComplete)); //this will launch the coroutine on our instance
 
  }
 
 }


Now we modify getFile to call that code and pass on your closure:

     public static void getFile(string path, Action<string> onComplete)
     {
            StaticCoroutine.DoCoroutine(DoGetFile(path, onComplete));
     }

     static IEnumerator DoGetFile(string path, Action<string> onComplete)
     {
            var www = new WWW(path);
            yield return www;
            onComplete(www.text);
     }

So to round up - there's a modified version of the code from that other thread that works the way we need it to. You've rewritten getFile to start the coroutine and pass on the functions that you want to execute when the data is available later. Please note that in that function you now pass to getFile you can still access local variables - but do not access foreach or for loop variables unless you use a copy you take of them (it's a weird glitch).

Comment
Add comment · Show 6 · 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 catwoman · Mar 23, 2013 at 01:40 PM 0
Share

Thanks for your reply!! I'm learning a lot of stuff :)

I got this error:

NullReferenceException: Object reference not set to an instance of an object

I/Unity ( 2035): at StaticCoroutine.DoCoroutine

on the line: instance.StartCoroutine(instance.Perform(coroutine, onComplete));

ohhh: instance is null... but why? :)

avatar image catwoman · Mar 23, 2013 at 01:55 PM 0
Share

The Awake method of StaticCorouting is not called...hmmm

avatar image catwoman · Mar 23, 2013 at 02:04 PM 0
Share

ok nothing...now the awake is called...

avatar image whydoidoit · Mar 23, 2013 at 02:20 PM 0
Share

If the was called I don't see how instance could be null... Which is what that line appears to be complaining of. You did attach it to an active game object in your scene? Are you calling the getFile from a static constructor or as a variable allocation? That might happen before Awake.

avatar image catwoman · Mar 23, 2013 at 02:25 PM 0
Share

Yes the problem is that the script was not attached on an active object... but now it works... The routine is called, the file content is retrieved, and I can print the xml file on the log file!

I get another error, but I think it's not referred to this topic: when I run:

new XmlDocument.LoadXml(someString) it displays the following error:

XmlException: Text node cannot appear in this state. Line 1, position 1.

I/Unity ( 4939): at $$anonymous$$ono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace) [0x00000]

Anyway since I can see the content of the xml file in the someString variable, I think your first reply solved the problem!!

Thanks so much!

Show more comments

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

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

Getting strings from WWW 0 Answers

How to save to a database on application close 2 Answers

Ios problem with www class 2 Answers

Loading files via WWW class and Unity hangs for a few seconds if I use coroutines. If I don't, it works perfectly. What is going on? 1 Answer

loading asset bundle function 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