Movie script stops video half way ?

Hello Unity Answer community,
i have a C# script that when the game starts a video will play full screen and will stop when you hit the escape button or if the video is over and then level one will load but the video only plays half way, could you help me out and tell be what i am doing wrong.

using UnityEngine;
using System.Collections;

[RequireComponent(typeof (AudioSource))]
[RequireComponent(typeof(AudioListener))]
public class MoviePlayopaning : MonoBehaviour {
    public MovieTexture movie;

    private float _movieTimer;

    void OnGUI () {
	    GUI.DrawTexture(new Rect(0,0, Screen.width, Screen.height), movie);
	    movie.Play();
	
	   if(Input.GetKeyUp(KeyCode.Escape) || movie.duration < _movieTimer)  {
		   movie.Stop () ;
		   Application.LoadLevel ("Level1");
	   }
   }

   void Update () {
	  _movieTimer += Time.deltaTime;
   }
}

Thank you for your time and help,
Have a nice day/night.

1 Answer

1

I have a hunch that it’s your movie timer that’s stopping it; you see, OnGUI is executed more that once per update cycle, so your timer is essentially double what you think it is. Why don’t you just use !movie.isPlaying to load your new scene?

if(Input.GetKeyUp(KeyCode.Escape) || !movie.isPlaying)
    Application.LoadLevel("Level1");

Also, you might consider moving your logic to the Update Loop.

Hello, it gives me this error. error CS011: A namespace can only contain types and nameplate declarations

Can you post the line of code that its complaining about?

void Update () { _movieTimer += Time.deltaTime; } }

Take a look [at this question][1]. And make sure your update loop is contained within a class. [1]: http://answers.unity3d.com/questions/151609/a-namespace-can-only-contain-types-and-namespace-d.html

I fixed the problem and had the update loop is contained within a class but it wont load level and im not getting any errors ?