Calling Random Textures

I want to have a different texture appearing randomly on an object each time the game loads.

I can get the textures to change, i can get the randomness to work, but i cant think how to put them both together.

Here’s my current script:

var material1 : Material;
var material2 : Material;

//these represent two different texture diffuse maps
var randomno : int;

randomno = (Random.Range(1,4));
/generates a random no from 1-4

print(randomno);
//prints the no to the screen

function Update () {

renderer.sharedMaterial = material1;

// Currently just assigns Material1 to the object
//HERE I WANT TO BE ABLE TO USE THE RANDOM NUMBER GENERATED TO CHANGE THE TEXTURE. 
}

Im guessing it should be something like this:

material(randomno) or material[1], material[2]

but i cant seem to get the correct syntax. Any ideas apreciated.

Use an array of materials instead of making each one a separate variable. Then you can just pick one randomly from the array.

randomno = (Random.Range(1,4)); 
/generates a random no from 1-4

Actually that’s incorrect; it generates a random number from 1 to 3.

–Eric

Right I have looked in to arrays and my code now looks like this, but its producing this error:

SO im guessing i’ve put a school boy error somewhere…

var randomno :int;


function Start ()
{

var arr = new Array ();

   arr.Push ("material1");
   arr.Push ("material2");

   randomno = (Random.Range(1,3));

   print(arr[randomno]);

   renderer.sharedMaterial = arr[randomno];

}

By array of materials, I meant that you want to start like this:

var materials : Material[];

function Start () {
...

You don’t want to do this:

That’s just pushing a string onto an array; there’s no material involved.

–Eric

Hmm i’ve looked through the Unity reference manual and at a couple of forum pages but am not sure im fully understanding the array syntax you suggest properly.

Is this what you mean?

var materials : Material[];

function Start () { 

materials = (Random.Range(1,3));

}

renderer.sharedMaterial = material[materials];

Btw thanks for your time, it’s much appreciated

Getting there:

var materials : Material[];

function Start () {
   renderer.sharedMaterial = materials[Random.Range(0, materials.Length)];
}

As for the syntax, Unity uses .NET; you might have a look around the MSDN docs. Personally I find them convoluted compared to the Unity docs, but there is some introductory material there.

–Eric

Im still having trouble with this sadly,

I still cant seem to get this array working,

I did had success with the code below I put together which worked for a bit but now I cant select textures in the engine any more. They dotn appear in the inspector

var material1 : Material;
var material2 : Material;
var skinchoice = 1;



skinchoice = Random.Range(1, 3);

print(skinchoice);

		function Update () {

			if (skinchoice == 1)
			renderer.sharedMaterial = material1; 

					if (skinchoice == 2)
					renderer.sharedMaterial = material2; 

}

I’ve been going around in circles with this really and its so annoying to be so close!

[/code]

If you declare the array as a public variable you can assign its elements from the inspector. So, if you add:-

var materials: Material[];

…the “materials” array will be visible in the inspector and you can drag materials onto it, etc.

When you need to choose a material at random, use:-

var mat: Material = materials[Random.Range(0, materials.Length);

This will work regardless of how many materials are in the array.

I have this:

var materials: Material[];


function Start () {
  
var mat: Material = materials[Random.Range(0, 2)];
renderer.sharedMaterial = materials  ;

}

Still no joy as im getting this error:

Just to clarify maybe it is a modelling problem? I have a UV mapped character with no texture applied and the two separate texture files which i want to randomise. Is this the right way to go about this?

Cheers for everyone’s help.

You are trying to assign the materials array rather than just the chosen material. You need to use:-

renderer.sharedMaterial = mat;

Heres the code so far.

It randomly changes the texture in the inspector but not on the model and i get an error

var materials: Material[];


function Start () {
 
var mat: Material = materials[Random.Range(0, materials.Length)];
renderer.sharedMaterial = mat;

}

But i get an error:

Do all elements of the materials array have a valid material assigned to them (say, by dragging materials into the inspector)?