Flash Image once
I need to get an image in my game to quickly flash on the screen. I've done this so far:
IEnumerator BlinkNukeIcon(float delayInSecs) {
nukeIcon.color = Color.white;
yield return new WaitForSeconds(delayInSecs);
nukeIcon.color = Color.clear;
}
but when I StartCorountine
it then the image just appears and never disappears again.
Answer by Oscar-Bear · Apr 23, 2017 at 11:24 AM
Maybe this helps?
private Renderer nukeIcon;
public bool bom;
void Start ()
{
nukeIcon = GetComponent<Renderer>();
}
void Update ()
{
if (bom)
{
StartCoroutine(BlinkNukeIcon(2.0f));
}
}
IEnumerator BlinkNukeIcon(float delayInSecs)
{
nukeIcon.enabled = true;
yield return new WaitForSeconds(delayInSecs);
nukeIcon.enabled = false;
bom = false;
}
Make sure to set your sprite to false as default.
Hmmm strange, it works for me. Anyway this is another way of doing:
In Unity go to: GameObject > UI > Image. (This will create an image under a Canvas and a EventSystem)
On the GameObject Image u just created, go to the Image component, under Source Image select the image u want to display.
Change the position of the image to the position where u want to display it.
Add this: ShowImage script to the image:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ShowImage : $$anonymous$$onoBehaviour { public bool showNukeImage = false; private Image myImage;
void Start () { myImage = GetComponent<Image>(); myImage.enabled = false; } void Update () { if (showNukeImage) { StartCoroutine(BlinkNukeIcon(2.0f)); } } IEnumerator BlinkNukeIcon(float delayInSecs) { myImage.enabled = true; yield return new WaitForSeconds(delayInSecs); myImage.enabled = false; showNukeImage = false; } }
Press play
Now toggle the Show Nuke Image field on the ShowImage Component to true and see what happens.