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 /
This question was closed Jul 24, 2013 at 09:38 PM by clunk47 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Cameron 2 · May 07, 2010 at 06:39 PM · lightfade

Making a light that fades out one color and fades in a different color

As in start one color, fade out, fade in with a second color, fade out, and loop.

I'm new to Unity and kinda new to programming and very new to javascript, so I'm sure I'm doing this some weird way. I did some research but now I'm stuck. At one point I'd gotten it to fade out of the first color and fade in the second color but it would break after that.

If I run this script, It very quickly alternates between the two selected colors- obviously not the intended effect.

var color0 = Color.magenta; var color1 = Color.red; var duration = 1.0; var lastColor; var nowFading; var currentColorSum; var lastColorSum;

function Awake () { lastColor = color0; nowFading = true; }

function Update () {

 lastColorSum = lastColor.r + lastColor.g + lastColor.b;

 while(nowFading)
 {
     currentColorSum = light.color.r + light.color.g + light.color.b;
     if(currentColorSum <= 0)
     {
         nowFading = false;
     }

     else
     {
         if (lastColor == color0)
         {
             light.color -= color0 / duration * Time.deltaTime;
         }
         else if (lastColor == color1)
         {
             light.color -= color1 / duration * Time.deltaTime;
         }
         else
         {
             Debug.LogError("lastColor invalid", this);
         }
     }
 }


 while(!nowFading)
 {
     currentColorSum = light.color.r + light.color.g + light.color.b;
     if(currentColorSum >= lastColorSum)
     {
         if (lastColor == color0)
         {
             lastColor = color1;
             nowFading = true;
         }

         else if (lastColor == color1)
         {
             lastColor = color0;
             nowFading = true;
         }

         else
         {
             Debug.LogError("lastColor invalid", this);
         }
     }

     else
     {
         if (lastColor == color0)
         {
             light.color += color1 / duration * Time.deltaTime;
         }

         else if (lastColor == color1)
         {
             light.color += color0 / duration * Time.deltaTime;
         }

         else
         {
             Debug.LogError("lastColor invalid", this);
         }
     }
 }

}

How come it's not doing what I want? And am I using all of these lines of code to do something I could do with a simple math function?

Comment
Add comment
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

2 Replies

  • Sort: 
avatar image
5
Best Answer

Answer by Eric5h5 · May 07, 2010 at 07:45 PM

var color0 = Color.magenta; var color1 = Color.red; var duration = 1.0;

function Start () { while (true) { yield LerpLightColor (color0, Color.black); yield LerpLightColor (Color.black, color1); yield LerpLightColor (color1, Color.black); yield LerpLightColor (Color.black, color0); } }

function LerpLightColor (col1 : Color, col2 : Color) { var t = 0.0; var rate = 1.0/duration; while (t < 1.0) { t += Time.deltaTime * rate; light.color = Color.Lerp(col1, col2, t); yield; } }

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 Cameron 2 · May 07, 2010 at 11:29 PM 0
Share

Thanks, friend. Precisely what I meant.

avatar image
1

Answer by Mike 3 · May 07, 2010 at 06:45 PM

this starts a coroutine which fades between each color once every second

var col1 = Color.blue; var col2 = Color.green; var light : Light; var duration : float = 1.0;

function Start() { ColorFunction(); }

function ColorFunction() {

 var t = 1.0;

 while(true)
 {
     light.color = Color.Lerp(col1, col2, Mathf.PingPong(t,1));
     yield;
     t += Time.deltaTime / duration;
 }

}

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 Cameron 2 · May 07, 2010 at 06:55 PM 0
Share

Thank you for the very quick response.

This is actually the type of code I had started out with. The problem was I didn't want to get that sort of blending of colors effect that it gets when fading between colors. I wanted to modify it so that it would start at one color, fade all the way out (so that the light was "off"), and then fade in to a different color, and repeat that.

Sorry, I knew that would be confusing, I should've tried to be more clear.

Also, you don't have to declare light as a var since it is referencing the GameObject you're attached to. You actually can't run it that way

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Shader errors 0 Answers

Hardlight for spotlight 0 Answers

Script flares onto lights? 1 Answer

Check if an object has a light shining on it 1 Answer


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