- Home /
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.
a couple of things:
Start()
is called exactly once, which probably isn't what you need for fading over time...consider putting it in Update()
you're calling Strike()
recursively... that's why the code appears to stop.
there a many examples of how to fade colors... one of the easiest ways is to use one of the many tween libraries which can produce the desired effects. take a look at doTween
You should seriously consider writing a shader for this. Since it is a flat color, and you are just adjusting alpha; you can do this in a very fast shader, and you'll have more control in the end. I am unsure how to connect a shader to the camera though.
I'll be looking into shaders, and maybe even update the lightning to be a shader later on if that'd be the most efficient way of doing this. However, at the moment, I'll just consider this a coding exercise.
Answer by Bonfire-Boy · Oct 31, 2015 at 12:14 AM
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;
}
Worked perfectly!
I figured that the For loop would increment each frame, not each processor tick, but thinking back, the former wouldn't be very practical design. I also know now what coroutines are, which I'd heard of but not yet learnt. Thanks!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Renderer on object disabled after level reload 1 Answer
foreach (or for-next loop) not updating local values 1 Answer
Why is this script I made crashing unity 3 Answers