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