Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KotMurchik · Nov 14, 2019 at 09:51 PM · c#coroutinethreading

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;
     }
 }
 
 
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KotMurchik · Nov 15, 2019 at 08:10 AM 1
Share

Thank you! I use this script https://stackoverflow.com/a/41333540 and just call UnityThread.executeCoroutine(GetTexture(task.Result.OriginalString));

avatar image Bunny83 KotMurchik · Nov 15, 2019 at 02:10 PM 0
Share

Yes, that's a better dispatcher since it only locks for copying the scheduled tasks into a temp list.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

124 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Limiting the execution time of a lua script? 0 Answers

How can I save and load a Coroutine state ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges