- Home /
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";
}
});
}
}
is line 23 supposed to look like that?
And i suppose you could try this
yield return new WaitForSeconds(2);
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
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.
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.
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.
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!
Your answer
Follow this Question
Related Questions
IEnumerable in Unity 1 Answer
problems with damage over time script 2 Answers
WaitForSeconds not working C# 2 Answers