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
2
Question by save · Oct 05, 2011 at 11:10 PM · serverwwwyield

When using yield and WWW-class the function wont run

Having a bit of trouble with contacting something on a server. Lets say I have a script like this:

 static function postSomething () {
     Debug.Log("Hello World");
     var thePost : WWW = new WWW ("http://server.com/?foo=bar");
     yield thePost; //Yield makes the function never shoot off
     if(thePost.error) {
         Debug.Log("Error posting: " + thePost.error);
     }
 }

Now, if I remove the yield the function shoots off and prints "Hello World", but nothing actually gets posted from Unity onto the server. If I directly post it inside a browser, the php-script takes care of it so there's nothing wrong on that end. I experience the same issue with WWWForm. So my question is, what am I missing?

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

1 Reply

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

Answer by Steven-Walker · Oct 06, 2011 at 12:24 AM

Your problem is that the function is static. You cannot use yield in a static method. Instead, you'll need to create a script that can be instanced. I handle this in my code by using the singleton pattern. You can build a class with static access methods, but they delegate to a single instance of the object that is setup just once and a reference to it is stored.

 class MyExample extends Monobehaviour {
     static public var Instance : MyExample;
     
     function Init() {
         if(!Instance) {
             var g : GameObject = new GameObject("MyExample");
             Instance = g.AddComponent(typeof(MyExample)) as MyExample;
         }
     }
     
     function OnDestroy() {
         Instance = null;
     }
     
     static function postSomething() {
         Init();
         Instance._PostSomething();
     }
     
     function postSomething() {
         Debug.Log("Hello World");
         var thePost : WWW = new WWW ("http://server.com/?foo=bar");
         yield thePost; //Yield makes the function never shoot off
         if(thePost.error) {
             Debug.Log("Error posting: " + thePost.error);
         }
     }
 }
Comment
Add comment · Show 9 · 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 save · Oct 06, 2011 at 12:31 AM 0
Share

Fantastic, many thanks for the heads up and example!

avatar image Steven-Walker · Oct 06, 2011 at 12:43 AM 1
Share

I noticed a mistake in my code example above... the second postSomething routine should be named _PostSomething.

avatar image save · Oct 06, 2011 at 12:47 AM 0
Share

Roger that, nothing that Unity wouldn't have screamed about. :-) The big issue for me was that Unity was all nice and quiet, a "Did you just run a yield inside static function, have you learned nothing?" would have been nice!

avatar image ina · Jan 23, 2012 at 06:36 AM 0
Share

Also, when are you calling OnDestroy?

avatar image ina · Jan 23, 2012 at 06:37 AM 0
Share

So the idea is that you are calling init() for all the static functions in your class?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

yield www javascript 1 Answer

Yielding with WWW in Editor 9 Answers

Unity 5.6.7 - UnityWebRequest SSL/HTTPs 2 Answers

yield on www download or check www.isDone (in an update)? 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