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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Fading Colors

Discussion in 'Scripting' started by S3Critsss, May 28, 2020.

  1. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    I'm trying to make an object change between a set of colors using color.lerp but i have come across 2 problems : 1. It Skips colors like pink and cyan
    2. I am trying to make it fade but i dont know how
    i'm not that good at coding and any help would be appreciated.

    this is my script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CenterPeice_Color_Shifter : MonoBehaviour
    6. {
    7.     SpriteRenderer CenterPeice_Spriterenderer;
    8.  
    9.     [SerializeField] [Range(0f, 1f)] float lerpTime;
    10.     [SerializeField] Color[] myColors;
    11.  
    12.     int colorIndex = 0;
    13.  
    14.     float t = 0f;
    15.  
    16.     int len;
    17.  
    18.     public void Start()
    19.     {
    20.         CenterPeice_Spriterenderer = GetComponent <SpriteRenderer>();
    21.         len = myColors.Length;
    22.     }
    23.  
    24.     public void Update()
    25.     {
    26.         CenterPeice_Spriterenderer.material.color = Color.Lerp(CenterPeice_Spriterenderer.material.color, myColors[colorIndex], lerpTime);
    27.  
    28.         t = Mathf.Lerp(t, 1f, lerpTime * Time.deltaTime);
    29.         if (t>.9f)
    30.         {
    31.             t = 0f;
    32.             colorIndex++;
    33.             colorIndex = (colorIndex>=len)?0:colorIndex;
    34.         }
    35.     }
    36.  
    37. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    For step #1 you will need to put in Debug.Log() statements that show you which colorIndex number is being dealt with at any given time. You could also print the values of the color to figure out where your missing colors might be.

    For step #2, you can use Color.Lerp() and a a fractional value to tween from any Color to any other Color, such as tweening from Color.red to Color.black, or even to Color.clear if that's what you mean by fade.
     
  3. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    How would i do that?, sorry i'm not that good at coding
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    See how that Mathf.Lerp is done above? Color.Lerp does it the same way.

    You need to smoothly move a floating point value from 0.0f to 1.0f, changing it only a little bit each frame, perhaps in update, perhaps in a coroutine, and then feed it to Color.Lerp as the third argument to tween between the first two arguments.

    Actually, the docs have a great super-simple example:

    https://docs.unity3d.com/ScriptReference/Color.Lerp.html
     
  5. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    Thanks for your help but for the colors i have debug.log and its saying that all the colors are showing but they aren't, when it says color index 3 and 2 which are pink and cyan it shows color index 4 which is green