Video Player clip length value

Hey All,

I’m trying to create a script that would be a component of a game object with a video player attached. I’ve inserted an .mp4 into the public field for the video clip, and I need some info on that clip-specifically, its length, in seconds (so that I can input that value into another script, that would destroy the video player after the clip is finished). I know that there are different ways to accomplish this. I would be interested in either making this script work, or knowing how to grab the clip length from my other script (that triggers the video player), so that it contains a precise value of time for when the video player should stop rendering. Thanks!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;

public class VideoPlayerClipLength : MonoBehaviour

{
    private int lengthy;
    // Start is called before the first frame update
    void Start()
    {
      
    }

    // Update is called once per frame
    void Update()
    {


        lengthy = GetComponent<VideoPlayer.clip.length>();
        Console.WriteLine("Clip length"+ lengthy);
    }
}
  1. Only the class name goes inside the angle brackets. Anything from within that component goes outside of the parentheses (or after the variable if you store the component for later, which you should generally do if you’re going to reference it more than once)
  2. In this case, the length of the video won’t change, so you should just put that into Start() and not Update(); there’s no point in getting the length every frame.
  3. You’re trying to put it into an int, which won’t work; if you look at the documentation, it’s of type “double” (double-precision floating point number). In order to use it with a lot of Unity functions you’ll want to typecast that into a “float”.
private float lengthy;

void Start() {
      lengthy = (float) GetComponent<VideoPlayer>().clip.length;
}

Thank you @StarManta ! That worked, and you rule. I changed the variable of my other script to a float (from an int) and I was able to input the exact return value. Still trying to learn the syntax, and the order.