- Home /
Coroutine not starting
Hello. Why coroutine not started?
I use Firebase Realtime database to handle some data in my Unity mobile application It's work fine when i get url and press 'S'.
But I want start coroutine from code. Can anyone see what I am doing wrong? Thanks.
using Firebase;
using Firebase.Database;
using Firebase.Unity.Editor;
using UnityEngine;
using UnityEngine.UI;
using SimpleJSON;
using UnityEngine.Networking;
using System.Collections;
using Firebase.Storage;
using System.Threading.Tasks;
using System;
public class Connector : MonoBehaviour
{
[SerializeField] string _db;
[SerializeField] int _cuurentIndex; //current page index
[SerializeField] RawImage _rawTexture;
FirebaseStorage _storage;
[SerializeField] string _url;
void Start()
{
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl(_db);
_storage = FirebaseStorage.DefaultInstance;
FirebaseDatabase.DefaultInstance
.GetReference(_cuurentIndex.ToString())
.GetValueAsync().ContinueWith(task =>
{
if (task.IsFaulted)
{
print(task.Exception.Message);
}
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
print(snapshot.GetRawJsonValue());
ParseJson(snapshot.GetRawJsonValue());
}
});
}
// THIS WORKING WHEN I GET URL AND PRESS 'S'
private void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
StartCoroutine(GetTexture(_url));
}
}
private void ParseJson(string jsonString)
{
var N = JSON.Parse(jsonString);
print(N["url"].Value);
StorageReference gs_reference =
_storage.GetReferenceFromUrl(N["url"].Value);
gs_reference.GetDownloadUrlAsync().ContinueWith((Task<Uri> task) =>
{
if (!task.IsFaulted && !task.IsCanceled)
{
Debug.Log("Download URL: " + task.Result.OriginalString);
_url = task.Result.OriginalString; // for test
StartGetTexture(task.Result.OriginalString);
}
});
}
private void StartGetTexture(string url)
{
StartCoroutine(GetTexture(url));
}
IEnumerator GetTexture(string textureUrl)
{
UnityWebRequest www = UnityWebRequestTexture.GetTexture(textureUrl);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
Debug.Log(www.error);
else
_rawTexture.texture = ((DownloadHandlerTexture)www.downloadHandler).texture;
}
}
Answer by Bunny83 · Nov 15, 2019 at 04:14 AM
StartCoroutine is part of the Unity API and like most methods of the Unity API it is not thread safe and can only be used when called on the main thread.
If you need / want to use Tasks you have to provide a way to schedule your continuing code on the main thread. Something like that has been asked several times before. At the end of this article there's an example how you can schedule a delegate on the main thread by creating a dispatcher singleton. It's not the best implementation of such a dispatcher but it does the job.
Thank you! I use this script https://stackoverflow.com/a/41333540 and just call UnityThread.executeCoroutine(GetTexture(task.Result.OriginalString));
Yes, that's a better dispatcher since it only locks for copying the scheduled tasks into a temp list.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can I use coroutine to replicate .WaitOne() and .Set() behaviour? 1 Answer