Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved How do Events work? Can't seem to listen for Event

Discussion in 'Getting Started' started by torsov, Feb 7, 2022.

  1. torsov


    May 16, 2016
    I've been trying to learn how to make different kinds of UI elements. I'm currently trying to get a basic video player to work. I have several short video clips that I can trigger. I want to make it so that when one clip is finished, the next one is played automatically. I used the built-in Event system on the UI Elements to make things work so far, but now I figure I need my manager script to check with the Video Player component if the Event LoopPointReached has been triggered.

    I'm using Unity's built-in Video Player component, not an inherited class (did I say that right?).

    So what I have going is basically this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Video;
    5. using UnityEngine.UI;
    6. using UnityEngine.EventSystems;
    8. public class SimpleManager : MonoBehaviour
    10. {
    12. /*
    13.     The array lists the video clips that are in the playlist.
    14.     The index shows which location is currently being played.
    15.     vidPlay is a reference to the GameObject holding the Video Player component.
    16. */
    18.     public VideoClip[] video;
    19.     public int index = 0;
    20.     public GameObject vidPlay;
    22. void Start ()
    23.     {
    24.         vidPlay.GetComponent<VideoPlayer>().LoopPointReached += IncrementVideo();
    25.     }
    28. //The array listing the videos has an index number of 12.
    29.    public void IncrementVideo()
    30.     {
    31.         if (index != 11)
    32.         {
    33.             HandleInputData(index + 1);
    34.         }
    35.         else
    36.         {
    37.             PressedPlay();
    38.         }
    39.     }
    41. //HandleInputData sets the clip in [int] postion to play.
    43. // PressedPlay is called when the last video has played to move the user back to the video select screen.
    This does, for reasons I don't even faintly understand, not work. The error message is " 'VideoPlayer' does not contain a definition for 'LoopPointReached' and no accessible extension method 'LoopPointReached' accepting a first argument of type 'VideoPlayer' could be found (are you missing a using directive or an assembly reference?)". I'm guessing this means that I'm doing something wrong when I try to subscribe to the event?

    I am extrapolating this on the code that is here in the documentation (event subscription and methid call at the end of code example).

    Is anyone able to point me to a resource or something that can help me understand why this code does not run as intended?

    I have tried searching the forums a bit, but I can't seem to find threads who's solotion I have been able to implement.
  2. kittik


    Mar 6, 2015
    Have you tried loopPointReached instead of LoopPointReached? Is looping on the Video Player inspector settings set to true?
    torsov likes this.
  3. torsov


    May 16, 2016
    That seems to resolve the error that I got, but it threw another error instead: "No overload for 'IncrementVideo' matches delegate 'VideoPlayer.EventHandler'".

    Looping is set to false in the inspector, so the video should just end.
  4. torsov


    May 16, 2016
    I fixed this by setting the overload method to take the component as an argument, ie:

      public void IncrementVideo(VideoPlayer name)