Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Function not being called during task completion

Discussion in 'Scripting' started by EO-Altacc, Jan 7, 2020.

  1. EO-Altacc

    EO-Altacc

    Joined:
    Feb 19, 2019
    Posts:
    83
    Hello Unity Gurus,

    Got a newbie question regarding why a function within a task isn't being called when the task is complete.

    The Script is the following:

    public void DownloadVideo()
    {
    vidPath.GetFileAsync(saveLoc + "test.mp4").ContinueWith(task =>
    {
    if (!task.IsFaulted && !task.IsCanceled && task.IsCompleted)
    {
    Debug.Log("file downloaded");
    DisplayVid(); // <============ not being executed.
    }
    else
    {
    Debug.Log(task.Exception.ToString());
    }
    }
    );
    }

    public void DisplayVid() // <============== functions works on its own.
    {
    if(File.Exists(saveLoc + "test.mp4"))
    {
    vidSphere.url = saveLoc + "test.mp4";
    vidSphere.Play();
    Debug.Log("Play Video Function is called.");
    }
    else
    {
    Debug.Log("file doesn't exist!");
    }
    }

    The first function downloads a video from Google Firebase storage and stores it on the disk. The download works property and the file is written to the drive specified. When complete, the function gives a notice in the console, and is supposed to execute the DisplayVid() function as well - it fails to do the latter and I don't know why. The second function works fine on its own, say, when placed on a button. Just not when execute from the lambda expression task-complete. I've never had to use a lambda expression before, so I'm not sure what to look up. Thought I ask for assistance. Any help would be immensely appreciated.

    Thanks in advance :)
     
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Is your Debug.Log("file downloaded"); displayed? If not, find out why the If statement returns false, one of those values probably isn't what you expect, like

    Debug.Log ("IsCanceled = " + task.IsCanceled.toString());
     
  3. EO-Altacc

    EO-Altacc

    Joined:
    Feb 19, 2019
    Posts:
    83
    The debug log is displayed properly, yes. All the debug messages work. It's just the DisplayVideo() method that isn't being called.
     
    Last edited: Jan 8, 2020
  4. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    No, I'm not asking if Debug.Log works :) I'm suggesting to use it debug your logic. Write out the variable names at runtime, I suspect they are not what you think they are.
     
  5. EO-Altacc

    EO-Altacc

    Joined:
    Feb 19, 2019
    Posts:
    83
    I might be misunderstanding you, but the Debug.Log("File Downloaded") executes and displays in the editor console when the file is successfully downloaded. When there is no file to download (say I mislabel the file on purpose), I get the second debug message showing a FireBase specific error in the console.

    That really isn't the issue, though. I'm confused how the first line in the Lambda expression can execute but not the second line when they both share the same condition. Unless there is some 1-line limit to Lambda expressions I don't know about.
     
  6. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    It might be a threading issue and trying to access the main thread from the task thread. Use Try/Catch
     
  7. EO-Altacc

    EO-Altacc

    Joined:
    Feb 19, 2019
    Posts:
    83
    Did some more bug testing and I'm completely stomped.

    If I put a function before the Debug.Log (doesn't matter what function), all deblug.logs after stops working. Any debug.logs that are before the function works fine. Even more strangely, in the functions being called by this function, only the debug.log actions work. Any other action is ignored, as well as debug.logs that are called after it.

    Say I had this.

    public void DownloadVideo()
    {
    vidPath.GetFileAsync(saveLoc + "test.mp4").ContinueWith(task =>
    {
    else if (task.IsCompleted)
    {
    Debug.Log("Downloaded complete.");
    Debug.Log("More Test");
    TestMessages(); <========== this is the function being tested
    }
    else
    {
    Debug.Log(task.Exception.ToString());
    }
    }
    );
    }

    public void TestMessages()
    {
    Debug.Log("BOO");
    contextText.text = "Download Done";
    Debug.Log("HOO");
    }

    The console will display:

    Download Complete (I get the file)
    More Test
    BOO

    but skip the context.Text action and not display the Debug.log after (HOO). But if I call TestMessages() from any other function in the script, or say from a button click, it works fine.
    Basically, anything other than debug.logs fails to execute.

    Just what is going on here? Does a task not have access to variables and functions from the same script?
     
    Last edited: Jan 14, 2020
  8. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Try/Catch! You probably have an uncaught exception
     
  9. AaronBacon

    AaronBacon

    Joined:
    Sep 23, 2017
    Posts:
    3
    Posting this for anyone that comes across this, as I've spent the last 3 days trying to find the correct solution to this, and the answer as it turns out is pretty simple, and has to do with how Firebase handles threads. As I understand it, In order to run any external functions, you need to be on the Main thread, and Firebase creates its own thread to run Parallel to the game. The simple solution is to add
    Code (CSharp):
    1. using Firebase.Extensions;
    And then replace
    ContinueWith
    with
    ContinueWithOnMainThread


    I'm just glad I stumbled upon this Firebase article that explains it:
    https://firebase.blog/posts/2019/07/firebase-and-tasks-how-to-deal-with
     
    TSS1177, Sthuarty and Bunny83 like this.