Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by sid4 · Oct 31, 2016 at 10:41 PM · else

EASY FIX! How to make the screen flash for only a few seconds?

this script works but It stays stuck on red , I want it to flash/blink the red for a few second sonly each time not stay on the red. please help me fix my code thanks

c# 2d this is code in the beginning only..

     public Slider healthSlider;   // Reference to the UI's health bar.
     public Image damageImage;  // Reference to an image to flash on the screen on being hurt.
     public float flashSpeed = 5f;  // The speed the damageImage will fade at.
     public Color flashColour = new Color(1f, 0f, 0f, 0.1f); // The colour the damageImage is set to, to flash.
 
 
 
 
 
 
 
 =====================

this is code below regarding the if /else statement

 }
             }
             else
             {
                 // do something if not correct - for example
                 currentWord = 0; // force restart
                 damageImage.color = flashColour;
                 damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
                 //Destroy(gameObject, 1); // magic door closes - remove object
             }
         }
Comment
Add comment
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

3 Replies

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

Answer by TBruce · Nov 01, 2016 at 06:39 PM

This is an easy one. You do not need two coroutines. First add this to the list of public variables in your class

 public int numFlashes = 4;
 public float timeBetweenFlash = 0.5f;
 public Color flashColor = Color.red;

Now add this coroutine

 IEnumerator FlashInput(InputField input)
 {
     // save the InputField.textComponent color
     Color defaultColor = input.textComponent.color;
     for (int i = 0; i < numFlashes; i++)
     {
         // if the current color is the default color - change it to the flash color
         if (input.textComponent.color == defaultColor)
         {
             input.textComponent.color = flashColor;
         }
         else // otherwise change it back to the default color
         {
             input.textComponent.color = defaultColor;
         }
         yield return new WaitForSeconds(timeBetweenFlash);
     }
     Destroy(input.gameObject, 1); // magic door closes - remove object
     yield return new WaitForSeconds(1);
 }

Now just replace everything in your else block with this line

 StartCoroutine(FlashInput(input));
Comment
Add comment · Show 5 · 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 sid4 · Nov 01, 2016 at 09:33 PM 0
Share
 public class CodeWord
 {
avatar image TBruce sid4 · Nov 01, 2016 at 09:57 PM 0
Share

I am sorry to say but your above code is a mess. You have place a function within a function (the coroutine is defined inside the LockInput and the class ends early with extra code). It should look more like this. Important: It looks like you have been creating more that one version of the Werda class, it is important to not that though that is perfectly fine, the class CodeWord is global and you can only have one version ore you will receive an error.

avatar image sid4 TBruce · Nov 01, 2016 at 10:22 PM 0
Share

and its ok I tried putting it there also but I prob mixed something up I will redo and re check that and do that area now

il let u know how that turns out

Show more comments
avatar image
1

Answer by LucianoMacaDonati · Nov 01, 2016 at 12:16 AM

You're setting the color to "flashColour" (misspelled Color, btw) before using your Color.lerp (which will overwrite that). Besides that, you're always lerping to Color.clear (Transparent color). That's only half a single "blink".

I would make a Coroutine to lerp from a color to another over a period of time. Then I would put this inside a for-loop on another coroutine. You can call this function as an "event". You would not call this on your update. Here's an example of how I would do this:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 [RequireComponent(typeof(Image))]
 public class ErrorBlink : MonoBehaviour
 {
     Image blinkImage;
     Color startColor;
 
     void Start()
     {
         blinkImage = GetComponent<Image>();
         startColor = blinkImage.color;
     }
 
     public void BlinkRed()
     {
         StartCoroutine(Blink(Color.red, 5, 1));
     }
 
     public IEnumerator ColorLerpTo(Color _color, float _duration)
     {
         float elapsedTime = 0.0f;
         while (elapsedTime < _duration)
         {
             blinkImage.color = Color.Lerp(blinkImage.color, _color, (elapsedTime / _duration));
             elapsedTime += Time.deltaTime;
             yield return new WaitForEndOfFrame();
         }
     }
 
     public IEnumerator Blink(Color _blinkIn, int _blinkCount, float _totalBlinkDuration)
     {
         // We divide the whole duration for the ammount of blinks we will perform
         float fractionalBlinkDuration = _totalBlinkDuration / _blinkCount;
 
         for (int blinked = 0; blinked < _blinkCount; blinked++)
         {
             // Each blink needs 2 lerps: we give each lerp half of the duration allocated for 1 blink
             float halfFractionalDuration = fractionalBlinkDuration * 0.5f;
 
             // Lerp to the color
             yield return StartCoroutine(ColorLerpTo(_blinkIn, halfFractionalDuration));
 
             // Lerp to transparent
             StartCoroutine(ColorLerpTo(Color.clear, halfFractionalDuration));
         }
     }
 }

The cool thing about this, is that you can put it on a different Class and attach it to whatever you want to. Feel free to adapt my code to whatever you need.

Enjoy !

Comment
Add comment · Show 11 · 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 sid4 · Nov 01, 2016 at 12:30 AM 0
Share

its not working I know the colour was misspelled I referenced it that way also. though

but anyway your code isn't working

I don see the if anywhere? I posted a if statement so on if else it would flash red yours has no including of my code

again I need this to blink on the else if statement I posted.

avatar image LucianoMacaDonati sid4 · Nov 01, 2016 at 12:37 AM 0
Share

$$anonymous$$y response took effort and time: It's good manners to start with a "Thank you" when someone helps you for free. Besides that, my code shows you the concepts you need to understand to effectively use Lerp, Coroutines and more. I won't (and I don't think that's the purpose of this forum) just give you the answer.

Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.

avatar image sid4 LucianoMacaDonati · Nov 01, 2016 at 12:41 AM 0
Share

sorry thank you its just im stuck on this coding and still learning c# and this is sticking me bad c# block to the max

Show more comments
avatar image sid4 · Nov 01, 2016 at 12:35 AM 0
Share

this is the whole script I have the if is in the end using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.IO;

 public class CodeWord
 {
     public HashSet<string> hash;
 }
 
 public class werda3 :  $$anonymous$$onoBehaviour
 {
     public InputField inputField; // set this as an InputField not a GameObject
     public string wordsFile;
     public Slider healthSlider; // Reference to the UI's health bar.
     public Image damageImage;// Reference to an image to flash on the screen on being hurt.
     public float flashSpeed = 5f;  // The speed the damageImage will fade at.
     public Color flashColour = new Color(1f, 0f, 0f, 0.1f);     // The colour the damageImage is set to, to flash.
 // this will allow you to change the number of correct inputs needed (mainly for testing/gb\\CodeWords\\words" + (i + 1).ToString() + ".txt";
             string[]b            if (inputField != null)
         {
             g.Contains(input.text)) // check if the HashSet contains the code they entered 
             {
                 g                else
             {g        {
     }
 }
avatar image LucianoMacaDonati sid4 · Nov 01, 2016 at 12:59 AM 0
Share

In your "else" you need to check if the color is already red. If not, lerp to it. If it is already red, then lerp to Color.clear. The problem is that this way it will only "blink" one time. Try adding a "counter" to it.

Your code is doing so many different things that it makes your work super hard to debug. Next time try breaking everything into different pieces, it's much easier. Good luck.

PS: If you read my answer and give it enough thought, it does actually solve your problem. It's just a more compact, different way (I prefer it). So make sure to mark it as correct when you're done investigating. Thank you !

avatar image sid4 LucianoMacaDonati · Nov 01, 2016 at 01:04 AM 0
Share

thanks I will let u know by tomorrow I will add correct it in there and test fix tomorrow

have a goodnight

Show more comments
avatar image
0

Answer by sid4 · Nov 01, 2016 at 10:21 PM

mavina ye s u right I was going to task u but I spent a couple hours and figured that out a couple days ago, when u send me the global I couldn't create other scripts then I read the error and realized I would comment it out and it worked so yes I realized I can only use 1 at a time

is there any way to allow it on them all though it s a pain to go back and forth and comment them out constantly

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 LucianoMacaDonati · Nov 01, 2016 at 10:30 PM 0
Share

$$anonymous$$ake sure to mark the question as Answered when you're done here.

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

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Else section in if statement not executing? 0 Answers

code so if 1 enemy isnt killed every 4 seconds player dies? 1 Answer

Laser System - "If" statement conflict 1 Answer

Cursor Lock Code w/ if, else if, and else Statement Errors 1 Answer

code so if 1 enemy isnt killed every 4 seconds player dies? 0 Answers


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