Changing an animated texture on a plane.

Hey everyone. What I am trying to do is change a texture on a plane that I am using for my front end.
In the scene I have: A plane with the texture, A menu script that has all my GUI info a camera and a light.
On the plane there is a material with a .mov file and there is a piece of the code on the plane that lets you use an animated texture.

The code:

function Start () {
renderer.material.mainTexture.Play ();
}

Now what I am looking to do is, when the player clicks a button in the Main Menu Script, it will swap this texture with a different animated texture. I have tried a lot of different stuff but where I am at now is.

var mainBG : Texture;
var gamepadBG : Texture;

function Awake() {
timeScale = Time.timeScale;
director = GameObject.Find(“Director”).GetComponent(“Director”);
volumeSlider = director.volume;
}

function HowToPlayScreenJoyControls() { // This will be the first screen for the how to play part
if (GUILayout.Button(“>>>”, GUI.skin.label)) {
screen = “HowToPlayKey”;
}
GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); // infos that will be relavent eventually
GUILayout.Label(“How To Play: Game Pad”);
GUILayout.FlexibleSpace(); GUILayout.EndHorizontal();

var bg : GameObject = GameObject.Find(“Background”).GetComponent(“Background”);
bg.renderer.material.mainTexture = gamepadBG;
// With this last line I am getting a NullReferenceException: Object reference not set to an instance of an object.
}

There is obviously more code than what I have put but these are the only pieces that are interacting with the texture I am trying to change. I am still fairly new to scripting so my apologies if I am missing something obvious.

Also would it be better to put the first bit of code into the Main Menu script and just put it all on the background instead of having it on a separate game object?

Thanks for any help.

Have you dragged a texture into gamepadBG in the inspector?

Yes I have, all the textures have textures in the inspector. One other thing is you have to re enable the texture animation loop on the texture itself. I was wondering if there is a piece of code I can put in that will just turn looping back on every time the scene is loaded.

Thanks.

“NullReferenceException: Object reference not set to an instance of an object.” basically means “I don’t have what you are trying to use…”

So either it doesn’t have something for gamepadBG or it didn’t find bg.

You can do something like:

if(!gamepadBG){
	print("Missing gamepadBG");
	return;
}
var bg : GameObject = GameObject.Find("Background").GetComponent("Backgr ound");
if(bg){
	bg.renderer.material.mainTexture = gamepadBG;
}
else{
	print("Missing bg");
}

Alright I added that and it is returning “Missing BG”. So for one reason or another it is unable to find the background game object. I tried taging it as well but that didn’t work either.

Well your code looks for a component “backgr ound” - if the material you want is on the gameobject ‘background’ then remove that getcomponent bit

I figured it out by changing some of the code and changing the textures to MovieTextures. Thanks for your help though, appreciate it.