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

Bug SpriteRenderer.color not working at runtime?

Discussion in '2D' started by G-RexStudio, Jul 31, 2023.

  1. G-RexStudio

    G-RexStudio

    Joined:
    May 21, 2022
    Posts:
    25
    Hello! I want my sprite to have an effect that would always change its color to any color through lerp, so I'm using this code method to see would it work:

    Code (CSharp):
    1. void Start()
    2.     {
    3.         colors = new Color32[7]
    4.         {
    5.             new Color(255, 0, 0, 255),
    6.             new Color(255, 165, 0, 255),
    7.             new Color(255, 255, 0, 255),
    8.             new Color(0, 255, 0, 255),
    9.             new Color(0, 0, 255, 255),
    10.             new Color(75, 0, 130, 255),
    11.             new Color(238, 0, 0, 255),
    12.         };
    13.         StartCoroutine(ColorCycle());
    14.     }
    And I tested. Obviously, the code is fine, but the sprite won't actually change it even if it's called directly. I have tested it with material and it works with material.color, but why not sprite?

    Code (CSharp):
    1. public IEnumerator ColorCycle()
    2.     {
    3.         int i = 0;
    4.         while (true)
    5.         {
    6.             for (float interpolant = 0f; interpolant < 1f; interpolant += 0.001f)
    7.             {
    8.                 sprite.color = Color.Lerp(colors[i % 7], colors[(i + 1) % 7], interpolant);
    9.                 yield return null;
    10.             }
    11.             i++;
    12.         }
    13.     }
    If anyone knows a solution to this, I will be pleased
     
  2. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    143
    the main mistake is at
    these values; the Color values are ranged between [0,1], so fixing that will probably solve all your problems https://docs.unity3d.com/ScriptReference/Color.html

    additionally,
    You are doing a frame rate dependent work(
    ), so you should multiply the interpolant's change rate with Time.DeltaTime.

    another troubling issue is at
    this is just a bit too low of a float value to add, multiplying it with Time.DeltaTime will result in an even smaller addend; which may cause some floating point precision errors; I would probably get the t value of the lerp method by using frac(or %1f) on Time.time https://docs.unity3d.com/ScriptReference/Time-time.html . You'll of course have to change how the for loop ends too etc.
     
    FaithlessOne likes this.
  3. FaithlessOne

    FaithlessOne

    Joined:
    Jun 19, 2017
    Posts:
    256
    venediklee likes this.
  4. G-RexStudio

    G-RexStudio

    Joined:
    May 21, 2022
    Posts:
    25
    I found the problem! Althrough thanks @venediklee and @FaithlessOne for very helpful informations

    As I said above, the code was fine, so there are no errors in the code attachment.
    However, the problem is that I'm using a custom material that can't draw custom colors, it's actually a billboard material

    I hope I would understand to fix the custom material so that everything would work fine
    I'm very sorry for my confusion
     
    FaithlessOne and venediklee like this.