- Home /
How do I load a new scene after video?
I got my script to play a video when the scene starts, but how to I get it to load a new scene when it ends?
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(AudioSource))]
public class MyMovie : MonoBehaviour {
// Use this for initialization
void Start () {
Renderer r = GetComponent<Renderer>();
MovieTexture myMovie = (MovieTexture)r.material.mainTexture;
GetComponent<AudioSource>().clip = myMovie.audioClip;
GetComponent<AudioSource>().Play ();
myMovie.Play ();
}
// Update is called once per frame
void Update () {
}
}
Found a similar question. Look at this answer: http://answers.unity.com/answers/1479738/view.html
Answer by Lunar Soul · Jun 08, 2015 at 04:29 PM
Seems like the easiest way to do it is to take the exact duration of the video then make a timer at the start of the scene that contains the duration then when the timer ends load the new scene
Answer by YoungDeveloper · Jun 08, 2015 at 04:34 PM
Get movie duration or clip length, launch coroutine which waits that time and loads scene.
private void Start(){
StartCoroutine(WaitAndLoad(3f, "MyScene"));
}
private IEnumerator WaitAndLoad(float value, string scene) {
yield return new WaitForSeconds(value);
Application.LoadLevel(scene);
}
You could also use Invoke.
Hi I had the same problem and also used this method. sadly it is only good when I load my level for the first time. I added a "New Game" button to my main menu. when the button is pressed, it will load a specific scene with the video in it, video will play, get finished, and finally it will load the next intended scene after desired time. all is well. The problem is, if I go back to main menu and press "New Game" button again, the video will play, but at the end of the video, the scene will not change, it will just stay black like the last frame. I think the Script I used for this function is not working for the second time.
Any thoughts?
PS: I did not used Start or Awake function, also tried OnSceneLoaded and OnEnable with an Empty GameObject that can't be destroyed on load. but no luck.