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

Question Use the coroutine to change the scene 5 seconds after the video is over.

Discussion in 'Scripting' started by lsk111919, Sep 3, 2020.

  1. lsk111919


    Jul 8, 2020
    I want to change the scene 5 seconds after the video is finished using Coroutine. But what I've created is that the video starts and then the scene changes five seconds later. It hasn't been long since I started UNI.T. I want to learn a lot. I would really appreciate it if you could explain in detail what to do.
    Code (CSharp):
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine;
    6. using UnityEngine.Video;
    7. using UnityEngine.Timeline;
    9. public class EKKOVideo : MonoBehaviour
    10. {
    11.     public VideoPlayer mVideoPlayer = null;
    13.     void Start()
    14.     {
    15.         StartCoroutine(Hal());
    17.     }
    19.     IEnumerator Hal()
    20.     {
    21.         hama();
    22.         yield return new WaitForSeconds(5.0f);
    23.         saza();
    24.     }
    25.     void App(VideoPlayer player)
    26.     {
    27.         saza();
    28.     }
    30.     void hama()
    31.     {
    32.         mVideoPlayer.loopPointReached += App;
    33.     }
    35.     void saza()
    36.     {
    37.         SceneManager.LoadScene("MainScenes");
    38.     }
    40. }
  2. Yoreki


    Apr 10, 2019
    VideoPlayer has a length property in seconds. Just add that to your 5 seconds.
    So something long those lines: new WaitForSeconds(5.0f + (float)mVideoPlayer.length)

    But also, you seem to be calling saza(), thus LoadScene, twice, right? I never used VideoPlayer, but loopPointReached seems to be a callback event once the video finishes, in which case you call App(), which calls saza(). If i'm correct you could just use App() to start a Coroutine with a 5 second delay, which then calls saza(). Then again i never used VideoPlayer so i may be wrong in this assumption.

    As a bit of general advice, you may want to look into naming conventions. In C# we write methods in UpperCamelCase, starting with an upper case letter. So hama() should be Hama() and so on. This helps making your code uniform to that of other programmers and thus makes it easier to read and maintain. It also helps differentiating between things like properties / methods and variables.
    Another thing would be to try and code in english. For any people unfamiliar with your native language, the method names may as well be A(), B(), C(). For longer code sniplets, this would make understanding the code unnecessarily difficult. In the end this decision is up to you, but i wouldnt wonder when there are not a lot of people replying to questions containing longer code sniplets if they are written in something other than engllish. People just wont bother.
    PraetorBlue likes this.