I need an answer ASAP! So, I have this script and it makes it play my movie, and I don’t know how to make it open a scene after the movie! The movie is 15 seconds long, and here is the script:
using UnityEngine;
using System.Collections;
public class VideoController : MonoBehaviour {
// Use this for initialization
void Start () {
MovieTexture Movie = renderer.material.mainTexture as MovieTexture;
Movie.Play ();
}
// Update is called once per frame
void Update () {
}
}
So, please respond as soon as possible! I need an answer fast!
A very simple way would be to use Invoke to call a function that loads the scene after the movie.
void Start()
{
// same as your Start method, but with the following addition
Invoke("LoadScene", Movie.duration + 0.1f); // calls the method 0.1 seconds AFTER the movie has finished.
}
void LoadScene()
{
Application.LoadLevel("sceneName");
}
EDIT: I’ve had a look at the scripting reference, and it turns out that MovieTexture.duration is only available once the movie starts playing - before that, it returns -1. That means the code I gave you is calling Invoke with a negative number!
So, either try putting the Invoke call after you instruct the movie to play, or take note of how long it is and then hard-code that in.
The Invoke method is one way to call a function after a time period, and is definitely the simplest way to do so. In this case, your Start method would probably be as follows:
void Start()
{
MovieTexture Movie = renderer.material.mainTexture as MovieTexture;
Movie.Play ();
Invoke("LoadScene", 15f); // this will call the LoadScene method in 15 seconds
}
That’s progress, if it’s not throwing any compiler errors!
Application.LoadLevel uses either the index/number or the name of the level. In my original reply, I put levelName in quotes; so if you replace ‘levelName’ with the actual name of your level, it should work.
Yep, I tried that, and it still didn’t work, I double checked to see the duration of the video, and it appeared to be 10 seconds, so I changed the script and it still doesn’t work…
using UnityEngine;
using System.Collections;
public class VideoController : MonoBehaviour {
// Use this for initialization
void Start () {
MovieTexture Movie = renderer.material.mainTexture as MovieTexture;
Movie.Play ();
Invoke("Menu", 10f); // this will call the LoadScene method in 15 seconds
}
// Update is called once per frame
void Update () {
}
}
Oh, I see what you meant. In order to Invoke a method, the method has to exist in the first place. Your script doesn’t have a method called “Menu”, so Invoke fails. What you need to do here is to add it:
// leave your Start loop as it is
void Menu()
{
// this will load a level named "myLevel"
Application.LoadLevel("myLevel");
}