- Home /
Making an image suddenly flash onto screen and then off again? new code (edit)
var showTime : float = 0.5; var delayTime : float = 0.5; //decimals are mapped to floats or doubles. Most of the time you will want float in Unity.
private var showImage : Boolean = false; // ^ System doesn't hurt, but you don't really need it here. private var cycle : int = 0;
function Start() { FlashImage(); }
function OnGUI() { if (showImage) { //Code to draw your image goes here private void LoadImage() { string pathPrefix = @"file://"; string pathImageAssets = @"C:\Users\Administrator\Desktop"; string pathSmall = @"small\"; string filename = @"NHE1copy"; string fileSuffix = @".jpg"; } }
function FlashImage() { while (true) { showImage = true; yield WaitForSeconds(showTime); showImage = false yield WaitForSeconds(delayTime); } }
function Update () { }
I formatted your code for you, but your brackets don't match up and you shouldn't embed named functions in other functions.
And, you have major language mixing. You have some js, some C#, and some possible Obj-C.
sorry thats my bad i've been scouring these forums for code to use and everytime i keep getting mixes of java and c#:-/ I'm fairly ok at java and c# but i just get confused about certain things....
I just want a code that displays a character (in this case its name is Emily) for a split second as well as a small sound because i'm trying to create a kind of horror level....
Could you maybe help me out? :) Be much appreciated
GOD I LOVE YOU hahaha So i'm gonna go give this a shot and report back with results ;) This is all java just so i know mkay umm...Say out of curiosity what if i had made my picture into a material and that material is now on a character that i wanted to flash on screen quickly then disappear...how would that be done...in a similar way or completely different?
Answer by Peter G · May 02, 2011 at 03:39 AM
So do you want it to flash on and off because I'm not exactly sure what is wrong. You have a few type errors in your variable declarations, and I'm not sure what the final yield is for, but besides for that Your code should show an image then remove it. So I'm assuming that you want it to flicker on and off?
var showTime : float = 0.5; var delayTime : float = 0.5; //decimals are mapped to floats or doubles. Most of the time you will want float in Unity.
private var showImage : Boolean = false; // ^ System doesn't hurt, but you don't really need it here. private var cycle : int = 0;
function Start() { FlashImage(); }
function OnGUI() { if (showImage) { //Code to draw your image goes here } }
function FlashImage() { while (true) { showImage = true; yield WaitForSeconds(showTime); showImage = false yield WaitForSeconds(delayTime); } }
All's I added was an infinite loop. while(true) is always true so the loop will continually repeat and it waits x seconds before changing state.
EDIT:
Ok now I see what you want to do. I'll leave the collision detection up to you because there are multiple ways to do that. So here is a function that you can call to make an image pop up on the screen. I am going to use a GUITexture simply because you will get better performance. Calling OnGUI every frame when 95% of the time you will get a false from the conditional check is just a waste of processing power. That and empty Update()
's just waste time.
class ImageFlasher extends MonoBehaviour { //Explicit Class declaration was done because *Mike* says that's what I have to do to do properties in js.
var image : Texture2D;
//This is the image to display on the screen.
var flashLength : float;
var imageScreenCoords : Rect;
//You will probably want to make this more adaptive to the user's screen.
private var imageObject : GUITexture;
//This is property syntax is js. It looks wierd, but it's a getter just like C#
function get ImageObject() : GUITexture {
if(imageObject == null) {
var gameObj : GameObject = new GameObject("Image Object");
gameObj.transform.localScale = new Vector3(0, 0, 1);
imageObject = gameObj.AddComponent(GUITexture);
imageObject.texture = image;
imageObject.pixelInset = imageScreenCoords;
}
return imageObject;
}
//This works, but if you are against properties (or my implementation) then you can change it.
//It is not a critical part of this example.
function FlashImage () {
ImageObject.enabled = true;
Invoke("HideImage", flashLength);
}
function HideImage () {
ImageObject.enabled = false;
}
}
Call FlashImage()
when you want to show your object. It enables the GUITexture to show up over the rest of the screen... it invokes HideImage()
flashLength seconds later. This hides the image.
Yeah i'm making a game level and when my player collider runs through another collider i have set i want the image to flash its a creepy girl image from "nightmare house" her names emily :)
I also would like a small audio clip to play when it flashes... just want it to flash on and off once :)
Answer by Peter G · May 02, 2011 at 10:00 PM
Answer number 2.
Unfortunately, using a material would require a completely different approach. GUI's don't work well with materials, so if you wanted some "special" effect such as an animation or complex color changes, then you will probably have to do something like this:
Create a plane with your material and point a camera at it.
Put the camera in a higher layer and set the clear flags to "Depth only". Have this camera only render your plane and have your main camera render everything else.
Then, using a similar code to the other answer, you can turn the camera on and off to have the image (plane) overlay the rest of the screen.
This code assumes that you have a camera facing a plane with the picture of your girl on it. The camera can be named whatever you want, but just make sure that you reflect the change in your code:
class ImageFlasher extends MonoBehaviour { //Explicit Class declaration was done because *Mike* says that's what I have to do to do properties in js.
var flashLength : float;
private var imageObject : Camera;
//This is property syntax is js. It looks wierd, but it's a getter just like C#
function get ImageFlashObject() : Camera {
if(imageObject == null) {
imageObject = GameObject.Find("YourCamera").camera;
}
return imageObject;
}
function FlashImage () {
ImageFlashObject.enabled = true;
Invoke("HideImage", flashLength);
}
function HideImage () {
ImageFlashObject.enabled = false;
}
}