Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Why color isnt working in JS

Discussion in 'Editor & General Support' started by BlakeGillman, Apr 15, 2014.

  1. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    I have a lerp system that changes the background color of the world to represent different times of day, but when I hit play the sky goes completely black, what did I do wrong?

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. public var myCamera : Camera;
    5.  
    6. public var colorTime : float = 0;
    7. public var turnTime = 0; /* 0 for day 1 for night */
    8.  
    9. var lerpedColor : Color = Color.cyan;
    10. public var dayC : Color;
    11. public var nightC : Color;
    12.  
    13. function Update() {
    14.     lerpedColor = Color.Lerp(dayC, nightC, Mathf.PingPong(Time.time, colorTime));
    15.     myCamera.backgroundColor = lerpedColor;
    16. }
    17.  
    for DayC and nightC I went into the components and edited the color there, but the color still remains black when i hit play.

    EDIT:
    Here's a pic of the problem with the component
    http://imgur.com/cJpCYdN

    I am not sure why lerped color changes to black and gets that odd long white line
     
    Last edited: Apr 15, 2014
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Your alpha values for both dayC and nightC are 0, making it invisible.
     
  3. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Are you sure you want this?
    Code (csharp):
    1. Mathf.PingPong(Time.time, colorTime)
    Especially because it makes no sense when colorTime is 0.
     
  4. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    @Dantus How else should I approach this? A lot of that script is going off of the help someone else gave me...

    @DanielQuick And how do I change the alpha?
     
    Last edited: Apr 15, 2014
  5. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    If you look at the scripting reference entry, you'll see that it will return a value between 0 and the second parameter. In this case, since you are getting a value to use in a Lerp, you want a value between 0 and one. This means that the second parameter should be 1.

    The same way you change the colors, there are 4 slides. Red, Green, Blue, and Alpha
     
  6. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    So you mean that it should be (Time.time, 1) ?

    colorTime is a variable I have it makes so it takes 225 seconds to get to...

    Oh in the script I have colorTime set to 0.
    I made it 225 through the component side panel thing

    Thanks, worked perfectly! I feel kinda stupid lol.
     
    Last edited: Apr 15, 2014
  7. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Then the first parameter should be Time.time/colorTime
     
  8. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    lerpedColor = Color.Lerp(dayC, nightC, Mathf.PingPong(Time.time/colorTime));

    ERROR:

    The best overload for the method 'UnityEngine.Mathf.PingPong(float, float)' is not compatible with the argument list '(float)'.
     
  9. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    I said the first parameter, not the only parameter.

    You need to learn to use the scripting reference and follow the error messages instead of blindly testing things out.
     
  10. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412