Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DuckieMonster · May 02, 2011 at 03:09 AM · imageflashdisappearappear

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 () { }

Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Peter G · May 02, 2011 at 08:40 PM 0
Share

I formatted your code for you, but your brackets don't match up and you shouldn't embed named functions in other functions.

avatar image Peter G · May 02, 2011 at 08:42 PM 0
Share

And, you have major language mixing. You have some js, some C#, and some possible Obj-C.

avatar image DuckieMonster · May 02, 2011 at 08:57 PM 0
Share

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

avatar image DuckieMonster · May 02, 2011 at 09:23 PM 0
Share

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?

avatar image HolBol · May 02, 2011 at 09:58 PM 0
Share

Dude, it's JavaSCRIPT. Not Java. There's a difference.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DuckieMonster · May 02, 2011 at 05:07 PM 0
Share

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 :)

avatar image
1

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:

  1. Create a plane with your material and point a camera at it.

  2. 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.

  3. 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;
 }   

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Make enemy appear in front of player and then disappear at random times 2 Answers

how do i make an enemy appear then immediately dissapear? 1 Answer

Object appear and disappear on command 1 Answer

Text box and Image not appearing? 1 Answer

Enemy appear / disappear 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges