title: Get current frame Texture from VideoPlayer
body: <p>It is stated in this post <a href="https://stackoverflow.com/questions/41144054/using-new-unity-videoplayer-and-videoclip-api-to-play-video#">Using new Unity VideoPlayer and VideoClip API to play video</a> that one can "retrieve texture for each frame if needed"</p>
  
  <p>What's the proper way to get the current frame as a Texture2D, please?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>After the answer I did this but it's not working:</p>
  
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.Video;
  
  public class AverageColorFromTexture : MonoBehaviour {
  
      public VideoClip videoToPlay;
      public Light lSource;
  
      private Color targetColor;
      private VideoPlayer videoPlayer;
      private VideoSource videoSource;
      private Renderer rend;
      private Texture tex;
      private AudioSource audioSource;
  
      void Start()
      {
          Application.runInBackground = true;
          StartCoroutine(playVideo());
      }
  
      IEnumerator playVideo()
      {
  
          rend = GetComponent&lt;Renderer&gt;();
  
          videoPlayer = gameObject.AddComponent&lt;VideoPlayer&gt;();
          audioSource = gameObject.AddComponent&lt;AudioSource&gt;();
  
          //Disable Play on Awake for both Video and Audio
          videoPlayer.playOnAwake = false;
          audioSource.playOnAwake = false;
  
          videoPlayer.source = VideoSource.VideoClip;
          videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
          videoPlayer.EnableAudioTrack(0, true);
          videoPlayer.SetTargetAudioSource(0, audioSource);
  
          //Set video To Play then prepare Audio to prevent Buffering
          videoPlayer.clip = videoToPlay;
          videoPlayer.Prepare();
  
          //Wait until video is prepared
          while (!videoPlayer.isPrepared)
          {
              Debug.Log("Preparing Video");
              yield return null;
          }
          Debug.Log("Done Preparing Video");
  
          //Assign the Texture from Video to Material texture
          tex = videoPlayer.texture;
          rend.material.mainTexture = tex;
  
          //Enable new frame Event
          videoPlayer.sendFrameReadyEvents = true;
  
          //Subscribe to the new frame Event
          videoPlayer.frameReady += OnNewFrame;
  
          //Play Video
          videoPlayer.Play();
  
          //Play Sound
          audioSource.Play();
  
          Debug.Log("Playing Video");
          while (videoPlayer.isPlaying)
          {
              Debug.LogWarning("Video Time: " + Mathf.FloorToInt((float)videoPlayer.time));
              yield return null;
          }
          Debug.Log("Done Playing Video");
      }
  
      void OnNewFrame(VideoPlayer source, long frameIdx)
      {
          Texture2D videoFrame = (Texture2D)source.texture;
  
          targetColor = CalculateAverageColorFromTexture(videoFrame);
          lSource.color = targetColor ;
      }
  
  
      Color32 CalculateAverageColorFromTexture(Texture2D tex)
      {
          Color32[] texColors = tex.GetPixels32();
          int total = texColors.Length;
          float r = 0;
          float g = 0;
          float b = 0;
  
          for(int i = 0; i &lt; total; i++)
          {
              r += texColors[i].r;
              g += texColors[i].g;
              b += texColors[i].b;
          }
          return new Color32((byte)(r / total) , (byte)(g / total) , (byte)(b / total) , 0);
      }
  }
  </code></pre>