- Home /
Display Objects in the center of the screen and add commands
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.
Answer by robertbu · Aug 06, 2014 at 05:47 AM
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[i]; images[i] = images[j]; images[j] = temp; } DisplayImages(); }
function DisplayImages () { var i = 0; while(true) { renderer.material.mainTexture = images[i]; 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;
Thanks for the elaborate answer, it really helps me to understand the problem.. Yes i had thought about this option too but later assumed this process was lengthy.. I am trying your method now..
I was using the following code and getting till the point where i could make the debug log display the random name of these objects but then realized i was just calling the defined objects and not the gameobjects.. I thought i would post this code here earlier but then it was too messy to actually use now i understand the usage of the website.. sorry..
had images named exactly as the names given below so i was getting there but getting nowhere.. There was a debug log print command before i tried the instantiate that added to my confusion as it printed the random object name but displaying all objects on the screen where i exactly wanted them.. this code did not work, i guess i have syntax wrong somewhere.
pragma strict
int Size = 8; GameObject["Candy", "PokerChip", "WaterDrop", "WeedLeaf", "RubberDuck", "Orange", "Dice", "Cigarette"]; OnScreen = new GameObject[Size]; function Update() { for (int i = 0; i < Size; i++); { OnScreen[i] =GameObject.Instantiate (Resources.Load ("OnScreen")) as GameObject; OnScreen[i].transform.position = new Vector3(i * 0.32f,0,0); }
Anyway, I am trying your method now..
Hello,
I used your method and got to tghe point where the quad is displaying all the random images.. It works as it should!
This was my final code..
#pragma strict
public var OnScreen : Texture2D[] = new Texture2D[8];
public var timeBetweenImages = 5.0 / 8.0;
function Start () {
// Shuffle Images
var temp : Texture2D;
for (var i = 0; i < OnScreen.Length-1; i++) {
var j = Random.Range(i, OnScreen.Length);
temp = OnScreen[i];
OnScreen[i] = OnScreen[j];
OnScreen[j] = temp;
}
DisplayImages();
}
function DisplayImages () {
var i = 0;
while(true) {
renderer.material.mainTexture = OnScreen[i];
i = (i + 1) % OnScreen.Length;
yield WaitForSeconds(0.25);
}
}