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 Derf321 · Mar 13, 2015 at 04:40 AM · yield

Yield Statement Problems

I have this script Save() (Javascript) in which I send a message to a C# script to run the below function, which then sets the variable "receievedMessage" in Save() to "Success" or "Fail". Both scripts work great, but as soon as I add the 6th line with the yield, it throws me the error: "ArgumentException: method return type is incompatible". No idea why. I also tried putting the yield in it's own function and calling it, but then it tells me that the parent object is inactive (which it really is active, and gameObject.activeSelf says it's active), I don't have any code that would deactivate it.

Does this have something to do with the SendMessage? That's the only thing I can figure, but I don't know any other way to do a wait function. And before someone says to do it all in C#, the entire project is in javascript, I only needed one script in C# to use Parse.

Thanks, I appreciate help!

The JavaScript Script:

 public function Save(upload : boolean) {
     if (upload) {
         GameObject.Find("LevelManager").SendMessage("UploadToParse");        
         while (receivedMessage == null)
         {
             yield WaitForSeconds(2);
             Debug.Log("ReceivedMessage: " + receivedMessage);
         }
         if (receivedMessage == "Success")
              GameObject.Find ("ResultText").GetComponent(UI.Text).text = "Upload Success!";
          if (receivedMessage == "Failed")
             GameObject.Find ("ResultText").GetComponent(UI.Text).text = "Upload Failed: Check Connection or Save Offline";
     }
  }


And the C# Script:

 public class ParseHelperMethods : MonoBehaviour {
     public void UploadToParse() {
         Debug.Log("UPLOADING");
         string levelName = saveScript.levelName;
         string filePath = saveScript.filePath;
         var levelData = System.IO.File.ReadAllBytes(filePath + "/" + levelName + ".txt");
         var levelObject = new ParseObject("Levels");
         levelObject["author"] = ParseUser.CurrentUser;
         levelObject["levelName"] = levelName;
         levelObject["levelData"] = levelData;
         levelObject.SaveAsync().ContinueWith(task =>
                                              {
             if (task.IsCanceled || task.IsFaulted)
             {
                 Debug.Log("FAILED TO UPLOAD");
                 saveScript.receivedMessage = "Failed";
             }
             else
             {
                 Debug.Log("SUCCESSFULLY UPLOADED");
                 saveScript.receivedMessage = "Success";
             }
         });
     }
 }
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 ForeignGod · Mar 13, 2015 at 09:32 AM 0
Share

is line 23 supposed to look like that?

And i suppose you could try this

yield return new WaitForSeconds(2);

avatar image saud_ahmed020 · Mar 13, 2015 at 10:54 AM 0
Share

you can use Invoke method to do the same as you want.

Invoke("$$anonymous$$ethodNameThatYouWantToCallAfter2Seconds",2);

http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.Invoke.html

avatar image Derf321 · Mar 13, 2015 at 04:43 PM 0
Share

Crawlier, yes it's Parse syntax and I've tested it and it works. I tried your suggestion, but it won't compile like that (I think that's the syntax for C#).

Saud_ahmed020, I made a function with "return;" in it and used Invoke as you described, but it ends up freezing Unity.

2 Replies

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

Answer by turtleburger · Mar 13, 2015 at 10:54 AM

Not 100% sure what is happening, but I don't think you need to use yield WaitForSeconds(2); there. You have a while (receivedMessage == null) loop which should just halt your code and wait for receivedMessage to get set. Try yield return 0; or maybe yield return null; instead and see if that helps. I don't use Javascript, so I hope this is helpful.

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 Derf321 · Mar 13, 2015 at 04:51 PM 0
Share

I tried running it without the yield and it just freezes the program (as far as I know it just runs the while statement and doesn't run any other scripts). I also tried yield 0 and yield null (using "return" doesn't compile because it's C# syntax), both result in the "ArgumentException: method return type is incompatible" error.

avatar image
0

Answer by Derf321 · Mar 29, 2015 at 08:08 AM

I found that the problem came down to Parse. You can't call yields or a lot of other functions within a Parse statement. Thanks all for the replies, I'll mark turtleburgers' response as the answer as I did end up not using the yield in my case (but I did have to in the C# script), which was part of the problem. Cheers!

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.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

IEnumerable in Unity 1 Answer

problems with damage over time script 2 Answers

WaitForSeconds not working C# 2 Answers

4.6 UI.System: Triggering Layout Rebuild help 2 Answers

i need to delay an action but my code isnt working 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