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.
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];
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.
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!
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?