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
}
}
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));
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.
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
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 !
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.
$$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.
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
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 {
}
}
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 !
thanks I will let u know by tomorrow I will add correct it in there and test fix tomorrow
have a goodnight
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
$$anonymous$$ake sure to mark the question as Answered when you're done here.
Your answer
Follow this Question
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