Search Unity

How do you make a scene load after a movie played?

Discussion in 'Scripting' started by eric56379, Oct 29, 2014.

  1. eric56379

    eric56379

    Joined:
    Mar 11, 2014
    Posts:
    89
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class VideoController : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.  
    9.         MovieTexture Movie = renderer.material.mainTexture as MovieTexture;
    10.         Movie.Play ();
    11.  
    12.  
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update () {
    17.  
    18.     }
    19. }
    ----------------
    So, please respond as soon as possible! I need an answer fast!
     
  2. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    353
    A very simple way would be to use Invoke to call a function that loads the scene after the movie.

    Code (csharp):
    1.  
    2. void Start()
    3. {
    4.       // same as your Start method, but with the following addition
    5.       Invoke("LoadScene", Movie.duration + 0.1f);    // calls the method 0.1 seconds AFTER the movie has finished.
    6. }
    7.  
    8. void LoadScene()
    9. {
    10.         Application.LoadLevel("sceneName");
    11. }
    12.  
     
  3. eric56379

    eric56379

    Joined:
    Mar 11, 2014
    Posts:
    89
    Parsing error...
     
  4. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    353
    Okay, if there's a parsing error in that section, I suspect it's the second parameter of the Invoke statement. If that's the case, give this a try.

    Code (csharp):
    1.  
    2. void Start()
    3. {
    4.     // declare the MovieTexture here
    5.     float duration = Movie.duration;
    6.     Invoke("LoadScene", (duration + 0.1f) );
    7.     // play the movie
    8. }
    9.  
     
  5. eric56379

    eric56379

    Joined:
    Mar 11, 2014
    Posts:
    89
    Strange! It still gave a parsing error!
     
  6. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    353
    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! :oops:

    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.
     
    Last edited: Oct 30, 2014
  7. eric56379

    eric56379

    Joined:
    Mar 11, 2014
    Posts:
    89
    Okay, the movie is exactly 15 seconds long, and it plays... What do I add in? The invoke is something I never heard of.

    (Sorry, I don't know much coding)
     
    Last edited: Oct 30, 2014
  8. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    353
    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:
    Code (csharp):
    1.  
    2. void Start()
    3. {
    4.    MovieTexture Movie = renderer.material.mainTexture as MovieTexture;        
    5.    Movie.Play ();
    6.    Invoke("LoadScene", 15f);    // this will call the LoadScene method in 15 seconds
    7. }
    8.  
    Hope that sorts it out.
     
  9. eric56379

    eric56379

    Joined:
    Mar 11, 2014
    Posts:
    89
    Still isn't loading, but there are definitely no errors...

    What is happening that it is saying that the invoke cannot be called.
     
    Last edited: Oct 31, 2014
  10. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    353
    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.
     
  11. eric56379

    eric56379

    Joined:
    Mar 11, 2014
    Posts:
    89
    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...
     
  12. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    353
    Okay...could you post your entire code?
     
  13. eric56379

    eric56379

    Joined:
    Mar 11, 2014
    Posts:
    89
    Yes, here is the error...

    Trying to Invoke method: VideoController.Menu couldn't be called.

    (I will post a seperate post for the complete script)
     
  14. eric56379

    eric56379

    Joined:
    Mar 11, 2014
    Posts:
    89
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class VideoController : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.    
    9.         MovieTexture Movie = renderer.material.mainTexture as MovieTexture;
    10.         Movie.Play ();
    11.         Invoke("Menu", 10f);    // this will call the LoadScene method in 15 seconds
    12.  
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.    
    18.     }
    19. }
    20.  
     
  15. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    353
    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:
    Code (csharp):
    1.  
    2. // leave your Start loop as it is
    3.  
    4. void Menu()
    5. {
    6.     // this will load a level named "myLevel"
    7.     Application.LoadLevel("myLevel");    
    8. }
    9.