I'm trying to update text while my code performs a http request: code: Code (CSharp): public void SubmitTicket() { _txtTitle.text = "Please Wait..."; _Ticket.Product = _cbProducts.options[_cbProducts.value].text.ToString(); _Ticket.User_Email = _tbEmail.text; _Ticket.Issue_Description = _tbIssue.text; Validate_Submission(); if (_Valid) { //End Point string uri = _EndPoint + "api/WhiteBoXGaming/Post_SubmitS"; StartCoroutine(booleanwebrequest(uri, _Ticket)); if (!_Response) { _Core.Message_Box("Error: HTTP Request Failed", "Failed to submit your request. Please try again. If the problem persist, please contact support.", "OK"); } else { _Core.Message_Box("Information: Success", "Your request has been sent, please check your email to track your tickets progress. Thank you.", "OK"); } } //Reset Values _txtTitle.text = "Contact Support"; _Valid = true; } the text never changes. Do i need to do something different to get it to refresh or repaint or update the text? Not sure why its not working. _txtTitle is the text I want to change while it performs the task.
That isn't a coroutine or an async, so the entire thing is going to run in a single frame. If you change the text at the top of the function and then change it back to the original value at the bottom, then the text will always have the original value at the time any frame is rendered.
Wait to change the text back to the original value until the request finishes, or until a certain amount of time has passed (or both).
Code snippet would be helpful. BTW the co routine causes it to wait for a response otherwise I could not do line: 17.
Calling StartCoroutine() does NOT pause your function until the coroutine finishes. (IIRC the coroutine will run until its first yield, and then the calling function continues.) I'm not sure how your current code works, or whether it works, but I am 100% sure that StartCoroutine() does not pause for the coroutine to finish if the coroutine contains waits. For changing text when the request is done: If you have a working http request that correctly interprets the response you get, then by definition you must already have some code somewhere that waits for the request to finish (not shown in your OP). You can change your text there. If you don't already have working code for http requests, then updating text is not the problem you need to solve right now. For changing text after a fixed amount of time: If you need help to write a coroutine that says "wait X seconds, then do Y", then you do not understand coroutines at all. Go read an introductory tutorial.
Hmm I tried that code: Code (CSharp): IEnumerator booleanwebrequest(string uri, object obj) { try { string json = _Core.GenericObjectJson(obj); var client = new HttpClient(); var webRequest = WebRequest.Create(uri); _txtTitle.text = "Please Wait..."; webRequest.Method = "POST"; webRequest.ContentType = "application/json"; using (var streamWriter = new StreamWriter(webRequest.GetRequestStream())) { streamWriter.Write(json); } var httpResponse = (HttpWebResponse)webRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var result = streamReader.ReadToEnd(); _Response = Convert.ToBoolean(result); } } catch (Exception ex) { _Response = false; } _txtTitle.text = "Contact Support"; yield return _Response; } Still not working
Your coroutine doesn't appear to do anything other than execute completely in one go. Not sure why you're doing this in a coroutine this way. You might as well just make this a regular method, because that is essentially how it is behaving anyway. Coroutines are almost always used with a loop, where to prevent blocking the main thread you yield to space out execution of the entire loop across multiple frames. If you have no execution you're trying to spread out across multiple frames I don't see the value of this being a coroutine in the first place. Some short examples from the docs below. Notice every example includes a loop to be executed over multiple frames. https://docs.unity3d.com/Manual/Coroutines.html
Thanks for the replies but this does not solve my issue or answer my question. Telling someone their code sucks in a nutshell is counter productive. All I am trying to accomplish is a simple progress update/text change to notify the user processing is occurring. I assume a co routing is equal to a background worker in .Net Pretty sure this is a basic/fundamental question. More than one way to skin this cat. Please post helpful code snippets related to the problem please or post solutions snippets / solution directly related to the problem.
I am not inclined to write you a working example, because it would take more work than you seem to be willing to put in, so I assume that you have decided this problem isn't important enough to warrant that level of effort. Nope! Coroutines do not involve multi-threading. The fundamental reason that your notifications are not working is because your code for making the HTTP call is not correct, so you're trying to build a new layer on top of a foundation that was never doing what you wanted in the first place. I'm not sure exactly which of several possible flavors of "not correct" it is, because I haven't used that particular HTTP library before, so I'd have to learn how to use it myself in order to tell you how to use it correctly. The most likely possibilities are A) You are not waiting for the asynchronous operation to complete, or B) You are waiting, but are blocking the main Unity thread while you wait, which prevents Unity from doing anything else--including rendering the new text you want it to display