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. Dismiss Notice

(solved)how to change scene after Video has ended?

Discussion in 'Scripting' started by Aokkii, Oct 1, 2019.

  1. Aokkii

    Aokkii

    Joined:
    Nov 3, 2013
    Posts:
    117
    the video plays fine with the videoplayer component only. but once I attach this script (that it is suppose to do what i want), the video is skipped and void OnMovieEnded() is called without showing the video first.

    what am doing wrong in the code? is some other way to do it?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.Video;
    6.  
    7. public class videoscript : MonoBehaviour
    8. {
    9.  
    10.      VideoPlayer video;
    11.  
    12.     void Start()
    13.     {
    14.         video = GetComponent<VideoPlayer>();
    15.         video.Play();
    16.         StartCoroutine("WaitForMovieEnd");
    17.     }
    18.  
    19.  
    20.     public IEnumerator WaitForMovieEnd()
    21.     {
    22.         while (video.isPlaying)
    23.         {
    24.             yield return new WaitForEndOfFrame();
    25.          
    26.         }
    27.         OnMovieEnded();
    28.     }
    29.  
    30.      void OnMovieEnded()
    31.     {
    32.         SceneManager.LoadScene(0);
    33.     }
    34. }
    35.  
     
    Hamza199224 likes this.
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    Note the documentation for VideoPlayer.isPlaying:
    I.e. after calling Play, isPlaying is not immediately set to true while the video is being prepared and therefore your coroutine immediately calls OnMovieEnded.

    Either change the condition to "!video.isPrepared || video.isPlaying" or use the VideoPlayer.loopPointReached event, which will only fire when the video actually reached the end.
     
    ZaaRibe likes this.
  3. Aokkii

    Aokkii

    Joined:
    Nov 3, 2013
    Posts:
    117
    Thank you. Looppointreached worked.
     
  4. HerVap

    HerVap

    Joined:
    Oct 22, 2019
    Posts:
    5
    may you share the code Pipebknot? , that may help other users too :).
     
    BanXxX69 likes this.
  5. Aokkii

    Aokkii

    Joined:
    Nov 3, 2013
    Posts:
    117
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using UnityEngine.Video;
    4.  
    5. public class videoscript : MonoBehaviour
    6. {
    7.  
    8.      VideoPlayer video;
    9.  
    10.     void Awake()
    11.     {
    12.         video = GetComponent<VideoPlayer>();
    13.         video.Play();
    14.         video.loopPointReached += CheckOver;
    15.  
    16.          
    17.     }
    18.  
    19.  
    20.      void CheckOver(UnityEngine.Video.VideoPlayer vp)
    21.     {
    22.         SceneManager.LoadScene(1);//the scene that you want to load after the video has ended.
    23.     }
    24. }
    25.  
     
  6. MaxQuinonesSantander

    MaxQuinonesSantander

    Joined:
    Feb 20, 2020
    Posts:
    11
    This Code here ( i took it from somewhere in internet ) make it works too, you just need to say how many seconds has your video (in my case 375 and i let 3 sec wait, so 378) and then its take you to the scene you want =)

    using UnityEngine;
    using System.Collections;
    public class Test1 : MonoBehaviour
    {
    bool loadingStarted = false;
    float secondsLeft = 0;
    void Start()
    {
    StartCoroutine(DelayLoadLevel(378));
    }
    IEnumerator DelayLoadLevel(float seconds)
    {
    secondsLeft = seconds;
    loadingStarted = true;
    do
    {
    yield return new WaitForSeconds(1);
    } while (--secondsLeft > 0);
    Application.LoadLevel("//Here you write the name of your scene");
    }
    void OnGUI()
    {
    if (loadingStarted)
    GUI.Label(new Rect(0, 0, 100, 20), secondsLeft.ToString());
    }
    }
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
  8. ScionGlobe

    ScionGlobe

    Joined:
    Oct 28, 2020
    Posts:
    3
  9. unity_CAqm45pexFT04g

    unity_CAqm45pexFT04g

    Joined:
    May 30, 2022
    Posts:
    1
    Thank you so much, this worked perfectly! Even more years down the line