Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Adsolution · Oct 30, 2015 at 09:15 PM · c#for-loopwhile-loop

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · Oct 30, 2015 at 06:03 PM 0
Share

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

avatar image rageingnonsense · Oct 30, 2015 at 11:12 PM 1
Share

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.

avatar image Adsolution rageingnonsense · Oct 31, 2015 at 11:16 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Adsolution · Oct 31, 2015 at 11:14 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges