for/while loops not working, or not updating

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.

What makes you think that your Strike function stops? You can check this by adding some Debug.Log calls.

I wouldn’t expect it to stop, quite the opposite. The problem is that it will run to completion in a single frame, without ever returning control to the engine and allowing it to display the changes it’s made.

What you want is for the flow to return to the engine after each colour change, in order for it to be able to display the new colour.

You could use the Update() function for this but I would do it in a coroutine. In a coroutine, yield statements are used to temporarily relinquish control (until the next frame). It’s not a huge change to your code, something like this…

 void Start ()
 {
     Lightning = GetComponent<Image>();
     StartCoroutine (Strike(1, 1));
 }
 
IEnumerator 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
         yield return null;
     }
     // 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
         yield return null;
     }
     // Ensures flash ends up on 0
     flash = 0;
     Lightning.color = new Color(0.8f, 0.8f, 0.8f, flash); // Update overlay alpha
     yield return null;
}