random_elements

I create a cube, and i add the Mesh Renderer to the cube

set the Materials have 3 sizes, i put the image to each element

now, how can i randomly display the element??

simple to say, i want to randomly display image…thank you !

3 Answers

3

My idea would be to create an array of texture and using

renderer.material.mainTexture = texture*;*

with the index being chosen randomly with Random.Range();
Then you would have a function called every 20s or else that would generate a new value with maybe a check that the value is not the same as previous.

Well you need to create an array,
try:

var texture=Texture[];
var timer:int;
var changing:int;

function Start () {

texture[0]=yourTexture1;
texture[1]=yourTexture2;
texture[2]=yourTexture3;
texture[3]=yourTexture4;

var i = Random.Range(0,3);

this.renderer.material.mainTexture = texture*;*

changing=Time.time+20;

}

function Update () {
timer=Time.time; //Every frame timer gets the current time
if ((timer >= changing){ //Timer is compared to changing
var i = (Random.Range(0,3));
renderer.material.mainTexture = texture
changing=Time.time+20; //changing is given the actual time + 20. You can change the value here to make it longer or shorter.
}
}
I have not tried it so I can’t promise that it is flawless but that would be the idea.
Maybe someone who can decode with a blindfold on (there are a few of them wandering on this forum) will nicely correct my mistakes (or simply put me down :)).

var i = Array(“1”, “2”, “3”);

var a = i[Random.Range(0,2)];

function Start (){

var textures : Object = Resources.LoadAll(a);

var texture : Texture = textures[Random.Range(0, textures.Length)];

this.renderer.material.mainTexture = texture;

}

i get this form unity Script ref page

but i have some question to ask

what is —>Object ???

why use —>textures[Random.Range(0, textures.Length)]; ??