0

I tried to use a coroutine to download a get a text by UnityWebRequest.Get(url);

In the NetworkManager Class, I have these two exactly identical Coroutines but in different name:

public static IEnumerator DownloadTextCoroutine(string url, Action<string> callback)
    {
        List<word> newList = new List<word>();
        UnityWebRequest web = UnityWebRequest.Get(url);
        yield return web.SendWebRequest();

        if (web.result != UnityWebRequest.Result.Success)
        {
            Debug.LogError(web.error);
        }
        else
        {
            string Unprocessed = web.downloadHandler.text;
            callback?.Invoke(Unprocessed);
        }
    }

In the GameManager Class, I tried to run the coroutine and pass the text assests requested to a method initPlayerData(string A, string B), (i.e. this method is totally fine, so no worries about it). So In awake(), I run the coroutine. But it turns out the text assests are not downloaded.

 public void Awake()
        {
            if (instance == null)
            {
                instance = this;
                DontDestroyOnLoad(this.gameObject);
            }
            else
            {
                Destroy(this.gameObject);
            }

            Debug.Log(FirstTimeChecking.checkIsFirstTime());

            if (FirstTimeChecking.checkIsFirstTime())
            {
                StartCoroutine(NetworkManager.DownloadTextCoroutine("https://xxx/words.txt", result =>
                {
                    words = result;
                    isWordsDownloaded = true;
                }));

                StartCoroutine(NetworkManager.DownloadIdiomCoroutine("https://xxx/idioms.txt", result =>
                {
                    idioms = result;
                    isIdiomsDownloaded = true;
                }));

                StartCoroutine(WaitForDownloads());
            }
            else
            {
                playerData = playerDataLoader.playerData;
            }
        }

        private IEnumerator WaitForDownloads()
        {
            // Wait until both downloads have completed
            while (!isWordsDownloaded || !isIdiomsDownloaded)
            {
                yield return null;
            }

            // Run code after the downloads have completed
            initPlayerData(words, idioms);
        }

I expecct I can access 2 text assests and store them into a variable and pass it to initPlayerData(words, idioms); and the game shall run well.

I have tried this way and other many ways. If I put the coroutine in initPlayerData() [original], where there are some loops to handle the data, works. So I decided to seperate the Requesting Text process and the data process, i.e. initPlayerData(words, idioms);.

Thank you so much

  • Where is result set? I see you assigning stuff to its value but i dont see it getting set – BugFinder Aug 01 '23 at 09:08
  • The downloaded text format should be: ` Apple,\tAffle Orange,\tOmange ` The variables `words` and `idioms` will be passed into `initPlayerData(string A, string B)` The expected result should be: storing into a list of word, i.e. `List` where, ``` public class word{ public string wordName; public bool isCorrect; } ``` But now, with this code, this list is empty. – lemonofgreen Aug 01 '23 at 09:41
  • as I said.. where is result set? you have `words = result;` but you dont show result being set – BugFinder Aug 01 '23 at 10:01
  • ` string Unprocessed = web.downloadHandler.text; ` the result set is downloaded from the site. An example is { Apple,\tAffle Orange,\tOmange } – lemonofgreen Aug 01 '23 at 10:16
  • i dont think theres enough code to recreate it here, some debug logs should show where it disappears from – BugFinder Aug 01 '23 at 12:56
  • After adding some Debug.Log() I have found the issue, thank you so much! – lemonofgreen Aug 02 '23 at 06:26

0 Answers0