Hello, Im using javascript, I have a scene where i have 8 images that need to be flashed on the screen… say 8 images in 5 seconds…
I have no clue how to go about this… I am a 3d artist without scripting knowledge and have managed to get past making a start screen with buttons and a load level.
The only thing i really want to do right now is somehow display random images for a fixed amount of time and loop it in case i increase the rate…
Currently after everything i did failed, I dropped all images into game object 1 and then dropped that into a prefab… now i am just trying to instantiate various random gameobjects on center of screen… and then maybe define a color range and tint renderer with the defined color…
i also tried defining an array with the objects but i didnt get how i would create an array out of game objects.
Typically questions like this that have a feature list but no coding effort are rejected or closed. But after reading your question I find you are ‘out in the weeds’ with respect to how you are approaching the problem. So I’ll give you a bit of code to get you started. To use:
- Create and size a Quad (GameObject > Create Other > Quad) in the center of the screen.
- Add a material to the Quad
- Attach the following script to the Quad
- Select the Quad in the Hierarchy
- Click on the triangle next to the ‘Images’ variable in the Inspector
- Set the ‘size’ for ‘Images’ variable to however many pictures you want to use
- Drag and drop each texture into one of the new slots that opened up
- Set the ‘TimeBetweenImages’ variable
- Run
#pragma strict
public var images : Texture[];
public var timeBetweenImages = 5.0 / 8.0;
function Start () {
// Shuffle Images
var temp : Texture;
for (var i = 0; i < images.Length-1; i++) {
var j = Random.Range(i, images.Length);
temp = images*;*
images = images[j];
images[j] = temp;
}
DisplayImages();
}
function DisplayImages () {
var i = 0;
while(true) {
renderer.material.mainTexture = images*;*
i = (i + 1) % images.Length;
yield WaitForSeconds(timeBetweenImages);
}
}
Instead of an array of game objects, this code uses an array of textures, and changes the texture in the material. The way this code works is to first shuffle the images in the array in Start(), then to exectue a coroutine to display the images in (shuffled) order at the specified time apart. The shuffle assures that during a cycle, you will not get the same images twice (as would happen if you randomly picked an image). The use of a coroutine allows you to change ‘timeBetweenImages’ to speed up or slow down the display of images. If you will not be changing the timing, the code can be simplified by using InvokeRepeating().
Assuming you have a shader that supports tint color (only a fraction of the included shaders do), you can change the tint by setting the color in the material.:
renderer.material.color = Color.red;