0
votes

I've got a coroutine that should fetch an audioClip and then start another coroutine that plays that clip and waits until its finished so another clip could be played afterwards. The problem is - the program doesn't advance to the coroutine where I fetch the audioClip.

    private void RetrieveFromDatabase(int index)
    {
        Debug.Log($"{index} started...");
        FirebaseDatabase.DefaultInstance.GetReference("/Teams/" + TeamsSelection.teamSelected + "/").Child(index.ToString()).Child("userPosition").GetValueAsync()
           .ContinueWith(task => 
           {
               if (task.IsFaulted)
               {
                   //handle error
               }
               else if(task.IsCompleted)
               {
                   DataSnapshot snapshot = task.Result;
                   OnResponseGot(snapshot, index);
                   return;
               }
           });
    }

    private void OnResponseGot(DataSnapshot dataSnapshot, int index)
    {
        if (dataSnapshot == null || int.Parse(dataSnapshot.Value.ToString()) < 10)
        {
            Debug.Log("Trying again to retrieve " + index);
            RetrieveFromDatabase(index);
        }
        else
        {
            int userPos = int.Parse(dataSnapshot.Value.ToString());
            Debug.Log("retrieved userPos " + userPos);
            StartCoroutine(FetchWithUWR(userPos, index));
            //FetchWithResourceLoad(userPos, index);
        }
    }

    IEnumerator FetchWithUWR(int userPos, int index)
    {
        Debug.Log("Starting uwr for audioFiles");
        using (UnityWebRequest uwr = UnityWebRequestMultimedia.GetAudioClip($"C:/Users/Domas/Desktop/Projects/Wild_West/Assets/Resources/Audio/WAV_ENG/D{userPos}a", AudioType.WAV))
        {
            yield return uwr.SendWebRequest();
            Debug.Log("yielded a file!");
            if (uwr.isNetworkError)
            {
                Debug.Log(uwr.error);
            }
            else
            {
                AudioClip clip = DownloadHandlerAudioClip.GetContent(uwr);
                Debug.Log("Starting Coroutine " + index);
                StartCoroutine(PlayAudioClipAndStartRetrievingFromDatabase(index, clip));
            }
        }
    }

I don't even know where to start looking for a problem because I don't get any errors the program just keeps going but without the coroutine.

1
Do you have any errors on the Unity Console? - aalmigthy
No errors that originate from this script, there is an error that comes from FirebaseMonobehaviour but I doubt it has anything to do with this problem. But I will investigate. - DomasG
Errors can cancel script execution. Especially if the error is in the same frame, you can not trust any behaviour that comes after the error. Another approach would be to make breakpoints and check where the code stops executing. - aalmigthy
Can you provide the flow of methods? which method calls first? FirebaseDatabase Methods run on Unity Main thread? - AminSojoudi
Could you tell us the error message you get? - derHugo

1 Answers

0
votes

This problem can appear if you tried to start coroutine in other thread. In this case Firebase can handle exception and make it silent. Try use something like this to wait request in main thread:

    private bool requestDone;
    private DataSnapshot dataSnapshot;
    private int index;

    private void RetrieveFromDatabase(int index)
    {
        Debug.Log($"{index} started...");
        requestDone = false;
        StartCoroutine(WaitForResponce());

        FirebaseDatabase.DefaultInstance.GetReference("/Teams/" + TeamsSelection.teamSelected + "/").Child(index.ToString()).Child("userPosition").GetValueAsync()
           .ContinueWith(task => 
           {
               if (task.IsFaulted)
               {
                   //handle error
               }
               else if(task.IsCompleted)
               {
                   DataSnapshot snapshot = task.Result;
                   this.index = index;
                   thos.dataSnapshot = snapshot;
               }
               requestDone = true;
           });
    }

    private IEnumerator WaitForResponce()
    {
        while (!requestDone) yield return null;

        if (dataSnapshot == null || int.Parse(dataSnapshot.Value.ToString()) < 10)
        {
            Debug.Log("Trying again to retrieve " + index);
            RetrieveFromDatabase(index);
        }
        else
        {
            int userPos = int.Parse(dataSnapshot.Value.ToString());
            Debug.Log("retrieved userPos " + userPos);
            StartCoroutine(FetchWithUWR(userPos, index));
            //FetchWithResourceLoad(userPos, index);
        }
    }