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

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.

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

public class EKKOVideo : MonoBehaviour
{
    public VideoPlayer mVideoPlayer = null;

    void Start()
    {
        StartCoroutine(Hal());
       
    }

    IEnumerator Hal()
    {
        hama();
        yield return new WaitForSeconds(5.0f);
        saza();
    }
    void App(VideoPlayer player)
    {
        saza();
    }

    void hama()
    {
        mVideoPlayer.loopPointReached += App;
    }
   
    void saza()
    {
        SceneManager.LoadScene("MainScenes");
    }

}

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.

1 Like