- Home /
Coroutine not executing code after yield return <>.sendWebRequest
I am making app where user select country, and based on that country, I am creating new user account. I have UserOnStartHandler.cs script that looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.Networking;
using System.Text;
using System;
public class UserOnStartHandler : MonoBehaviour
{
public static UserOnStartHandler instance;
public Dropdown dropDown;
public Button confirmButton;
public Sprite[] Flags;
public string playerIdentifier { get; set; }
public List<int> usersIdList { get; set; }
public string PlayerNativeCountry = "";
public static PersonModel model = new PersonModel();
public CountryModel nativeCountry { get; set; }
public Action<CountryModel> countryDone;
private void Awake()
{
instance = this;
}
private void Start()
{
PopulateCountriesDropDownList();
HandleUserOnLoad();
}
void PopulateCountriesDropDownList()
{
dropDown.ClearOptions();
List<Dropdown.OptionData> flagItems = new List<Dropdown.OptionData>();
foreach (var flag in Flags)
{
var flagOption = new Dropdown.OptionData(flag.name, flag);
flagItems.Add(flagOption);
}
dropDown.AddOptions(flagItems);
}
public void HandleUserOnLoad()
{
if (PlayerPrefs.HasKey("Identifier"))
{
playerIdentifier = PlayerPrefs.GetString("Identifier");
}
else
{
StartCoroutine(GetUsersIdList());
}
IEnumerator GetUsersIdList()
{
List<int> idList = new List<int>();
UnityWebRequest getUsersIdList = UnityWebRequest.Get(WebReguests.getUsersIdList);
yield return getUsersIdList.SendWebRequest();
var data = getUsersIdList.downloadHandler.text;
if (getUsersIdList.isNetworkError || getUsersIdList.isHttpError)
{
Debug.LogError(getUsersIdList.error);
}
else
{
string[] result = data.Split('-');
for (int i = 0; i < result.Length; i++)
{
int idInt = int.Parse(result[i]);
idList.Add(idInt);
}
}
int userId = 0;
usersIdList = idList;
if (usersIdList.Count > 0)
{
userId = usersIdList[usersIdList.Count - 1] + 1;
}
else
{
userId = 1;
}
playerIdentifier = "Player" + userId;
PlayerPrefs.SetString("Identifier", playerIdentifier);
dropDown.gameObject.SetActive(true);
dropDown.onValueChanged.AddListener(delegate { SelectNativeCountry(dropDown); });
}
}
public void SelectNativeCountry(Dropdown dropdown)
{
int index = dropdown.value;
PlayerNativeCountry = dropdown.options[index].text;
confirmButton.gameObject.SetActive(true);
}
public void ConfirmButton(Button btn)
{
model.Identifier = playerIdentifier;
model.NickName = playerIdentifier;
model.NativeCountry = PlayerNativeCountry;
model = CreateUser(model);
}
public PersonModel CreateUser(PersonModel model)
{
BoatModel boat = new BoatModel();
StartCoroutine(GetNativeCountryCoroutine());
List<CountryModel> visitedCountries = new List<CountryModel>();
visitedCountries.Add(nativeCountry);
model.VisitedCountries = visitedCountries;
//...SOME OTHER CODE HERE...//
return model;
}
IEnumerator GetNativeCountryCoroutine()
{
WWWForm form = new WWWForm();
form.AddField("CountryName", model.NativeCountry);
using (UnityWebRequest getCountryByName =
UnityWebRequest.Post(WebReguests.getCountryByName, form))
{
var data = getCountryByName.downloadHandler.text;
// WAIT until the request has either failed or succeeded
yield return getCountryByName.SendWebRequest();
getCountryByName.SendWebRequest();
if (getCountryByName.isDone)
{
data = getCountryByName.downloadHandler.text;
}
if (getCountryByName.result != UnityWebRequest.Result.Success)
{
Debug.LogError(getCountryByName.error);
yield break;
}
else
{
var results = data.Split(',');
CountryModel country = new CountryModel();
if (int.TryParse(results[0], out var id))
{
country.id = id;
}
country.CountryName = results[1];
var flagBytes = Convert.FromBase64String(results[2]);
country.Flag = flagBytes;
var shapeBytes = Convert.FromBase64String(results[3]);
country.Shape = shapeBytes;
var woP = Convert.FromBase64String(results[4]);
country.WorldPosition = woP;
var coa = Convert.FromBase64String(results[5]);
country.CoatOfArms = coa;
var dishPicBytes = Convert.FromBase64String(results[6]);
country.DishPic = dishPicBytes;
var curiosityBytes = Convert.FromBase64String(results[7]);
country.CuriosityPic = curiosityBytes;
country.Continent = results[8];
country.Population = results[9];
country.Capital = results[10];
country.Language = results[11];
country.Currency = results[12];
country.Religion = results[13];
country.DishName = results[14];
nativeCountry = country;
}
}
}
}
So, everything is in the same class, and the problem is, in IEnumerator GetNativeCountryCoroutine(), after yield return getCountryByName.SendWebRequest() nothing happens, code just continue in public PersonModel CreateUser(PersonModel model), on line visitedCountries.Add(nativeCountry), and of course, nativeCountry is filled with null on all public properties. This is really frustrating, so please help, so I can continue my work, I spent so much time reading about this problem on line, but I cannot figure out how to fill nativeCountry with data.
@castelum85 I'm not sure about it myself, i just did a google search and by what i read here https://stackoverflow.com/questions/57988916/co-routine-killed-after-yield-return-unitywebrequest-sendwebrequest after the webrequest is successful you have to wait for the downloadHandler to be done, and in your code immediately after the web request response you try to parse the result.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Trouble Resuming after Yielding while Inside Coroutine 1 Answer
Waiting twice inside coroutine (C#) 2 Answers