I’m trying to create a lightning flash effect where a UI overlay covering the screen fades in and out on repeat (the repeat is just for testing), and here’s what I have:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
public class lightning : MonoBehaviour
Image Lightning;
float flash;
void Start ()
{
Lightning = GetComponent<Image>();
Strike(1, 1);
}
void Strike (float Intensity, float Duration)
{
// Flash fade-in
for (flash = 0; flash < Intensity; flash = flash + Time.deltaTime / Duration)
{
Lightning.color = new Color(0.8f, 0.8f, 0.8f, flash); // Update overlay alpha
}
// Flash fade out
for (flash = Intensity; flash > 0; flash = flash - Time.deltaTime / Duration)
{
Lightning.color = new Color(0.8f, 0.8f, 0.8f, flash); // Update overlay alpha
}
// Ensures flash ends up on 0
flash = 0;
// Restarts lightning strike
Strike(1, 1);
}
The issue from what I can tell is that only the very first increment in the for loop occurs, and then the code just stops. If I only write:
void Strike (float Intensity, float Duration)
{
flash = Intensity;
}
-Then it sets it accordingly.
I’ve only been using Unity for a few days so far, so maybe I don’t fully understand how updating works. I don’t need to use the Update() function here, do I? For note, I was using a While loop before but switched over to a For loop for the sake of cleanliness, but they yielded the exact same result.