Randomly select and apply a texture?

What we’re attempting to do is to randomly select a texture from a folder of textures in the asset list, and apply that texture to a game object. No matter what we try the texture doesn’t seem to get applied, even if we forgo randomly generating the number and specify a specific texture it still doesn’t apply. I feel like if we can figure out how to apply a texture programmatically we can get the rest to work but even after consulting the unity script reference we’re still pretty stuck. here is the code we are working with so far.

using UnityEngine;
using System.Collections;

public class TestTexture : MonoBehaviour {

	// Use this for initialization
	void Start () {
		GameObject.Find("Planet_3").renderer.material.SetTexture ("_MainTex",
			(Texture)Resources.Load ("Assets/Standard Assets/Terrain Assets/Planet Textures/0"));
	}
	
	// Update is called once per frame
	void Update () {
		GameObject.Find("Planet_3").renderer.material.SetTexture ("_MainTex",
			(Texture)Resources.Load ("Assets/Standard Assets/Terrain Assets/Planet Textures/0"));
	}
}

its supposed to work by generating a random number (all of the textures are named for numbers) and then to use that number to select the desired texture and apply it to the game object.

can anyone tell us what we’re doing wrong, and recommend how to fix it?

@DaveA is right: the page linked says that the resources must be in Resources folders - you specify the resource name, not its path, and Resources.Load searches in all Resources folders. You’re not getting error messages because SetTexture doesn’t throw any exception when the texture is null - it just does nothing. Move the textures to a Resources folder and just pass the asset name (“0”, in this case).

The rest of the code seems ok - I tested the simple script below and the textures were randomly changed every second:

var textures: Texture2D[]; // assign some textures here in the Inspector

function Start (){
	while (true){
		renderer.material.SetTexture ("_MainTex", textures[Random.Range(0, textures.Length)]);
		yield WaitForSeconds(1.0);
	}
}

NOTE: The error message you’ve got says that a script attached to some object was deleted, what has nothing to do with the texture problem.