Search Unity

Question Debug.Log deosn't work when called inside Video.EventHandler

Discussion in 'Scripting' started by Sound-Master, Jun 30, 2021.

  1. Sound-Master

    Sound-Master

    Joined:
    Aug 1, 2017
    Posts:
    48
    Hello, I hope somebody can hep me understand this issue I am having.

    I am using the following code to play a video clip when it has been prepared by the VideoPlayer, using the prepareCompleted event and the Prepare() function.

    The code works and the video works as expected. This is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Video;
    3. using System.Collections;
    4.  
    5. public class PlayVideo : MonoBehaviour
    6. {
    7.     VideoPlayer _arVideoPlayer;
    8.  
    9.     // Start is called before the first frame update
    10.     void Awake()
    11.     {
    12.         Debug.LogWarning("Video Prefab Instantiated!!!");
    13.         _arVideoPlayer = GameObject.Find("VideoPlayer").GetComponent<VideoPlayer>();
    14.         _arVideoPlayer.prepareCompleted += PlayVideoWhenReady;
    15.         _arVideoPlayer.Prepare();
    16.     }
    17.  
    18.     void PlayVideoWhenReady(VideoPlayer vPlayer)
    19.     {
    20.         Debug.LogWarning("Video is ready to play!!!"); //THIS DOES NOT PRINT IN CONSOLE OR LOGCAT
    21.         vPlayer.Play();
    22.     }
    23.  
    24. }
    However, the line
    Code (CSharp):
    1. Debug.LogWarning("Video is ready to play!!!");
    inside the
    Code (CSharp):
    1. PlayVideoWhenReady()
    function doesn't run (although the video plays correclty which means the play function is called).

    Can anybody explain why???

    Many Thanks

    Michele
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Sound-Master likes this.
  3. Sound-Master

    Sound-Master

    Joined:
    Aug 1, 2017
    Posts:
    48
    Thanks @Kurt-Dekker it is definitely not a problem of the console setup as I don't see the debug message in Logcat either, but I do see the debug massage in the Awake function.

    The issue of it being in another thread is more plausible.

    Do you know how to handle this situation? Why is it not printing if in another thread and what is the correct way of handling this?

    Many thanks
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I've never seen Debug.Log NOT show up, even from another thread, so my inclination is to think you are never getting the callback. Did you put a breakpoint in there and see it stop on the breakpoint?
     
    Joe-Censored likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If the problem is really caused by Debug.Log not working on some separate thread special case, you could consider creating a string ConcurrentQueue, and add your log message to that queue. In Update() you check if anything is in the ConcurrentQueue, and write it to Debug.Log there from the main thread. Just an idea.
     
    Kurt-Dekker likes this.