MovieTexture problem

Hi everybody ! I’m a french studient and I try to change a movie texture on a cylinder for a project.
But I’ve a problem.

There is my code :

using UnityEngine;
using System.Collections;


//[RequireComponent(typeof(AudioSource))]

public class VideoController : MonoBehaviour {

	// Use this for initialization
	void Start () {

		MovieTexture movie = renderer.material.mainTexture as MovieTexture;
		movie.Play ();
		//audio.Play ();
		
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetButtonDown ("Jump")) {
						MovieTexture movTex = Resources.Load<MovieTexture>("biere.mp4") as  MovieTexture;
						renderer.material.mainTexture = movTex;
			//AudioSource audio = Resources.Load<AudioSource>("biere.mp4") as AudioSource;
			movTex.Play();
				}
	}
}

and my error :
NullReferenceException: Object reference not set to an instance of an object
VideoController.Update () (at Assets/VideoController.cs:24)

This happens when I press the space bar when I want to change the video.

Thank you in advance for the help, good day :slight_smile:

I see three potential problems:

  1. You are cloning your material, see doc: Unity - Scripting API: Renderer.material So, you might not be playing the one you think you are.
  2. Resources.Load(“biere.mp4”) could be returning null! Are you sure it’s in the Resources folder?
  3. You are getting your materials confused because the Start() is assuming stuff about the Renderer we can’t see.

Try:

using UnityEngine;

[RequireComponent(typeof(Renderer))]
public class VideoController : MonoBehaviour
{
    public MovieTexture movieTextureFire;
    public MovieTexture movieTextureJump;

    public Material movieMaterialFire;
    public Material movieMaterialJump;

    // Note: The Awake function is called on all objects in the scene before any 
    // object's Start function is called. 
    void Awake()
    {
        // Setup your textures and materials
        // you could do this as Unity assets instead of at runtime
        if (movieTextureFire == null)
        {
            movieTextureFire = Resources.Load<MovieTexture>("fire");
        }

        if (movieMaterialFire == null)
        {
            movieMaterialFire = renderer.material; // This causes cloning
        }
        movieMaterialFire.mainTexture = movieTextureFire;

        if (movieMaterialFire == null
            || movieTextureFire == null)
        {
            Debug.LogWarning("The Fire movie is not correctly initialized!");
        }

        if (movieTextureJump == null)
        {
            movieTextureJump = Resources.Load<MovieTexture>("jump");
        }

        if (movieMaterialJump == null)
        {
            movieMaterialJump = renderer.material; // This causes cloning
        }
        movieMaterialJump.mainTexture = movieTextureJump;

        if (movieMaterialJump == null
            || movieTextureJump == null)
        {
            Debug.LogWarning("The Jump movie is not correctly initialized!");
        }
    }

    void Update()
    {
        if (Input.GetButtonDown("Jump"))
        {
            Debug.Log(string.Format("Jump! Playing = {0}", movieTextureJump.isPlaying));

            // Swap to the material and play the movie
            renderer.sharedMaterial = movieMaterialJump;
            movieTextureJump.Stop();  // Poor man's "Rewind"
            movieTextureJump.Play();
        }

        if (Input.GetButtonDown("Fire1"))
        {
            Debug.Log(string.Format("Fire1! Playing = {0}", movieTextureFire.isPlaying));

            // Swap to the material and play the movie
            renderer.sharedMaterial = movieMaterialFire;
            movieTextureFire.Stop();  // Poor man's "Rewind"
            movieTextureFire.Play();
        }
    }
}