Search Unity

Problem changing sprite color from script

Discussion in 'Image Effects' started by Annundur, Mar 3, 2019.

Thread Status:
Not open for further replies.
  1. Annundur

    Annundur

    Joined:
    Dec 18, 2018
    Posts:
    12
    Hi everyone. I'm trying to change the color of a sprite from a script attached to the same object, but it's not working.

    I'm developing a 2D game, and what I want is to place a semi-transparent color layer covering the full screen, to slightly change the tone of everything beneath (bluish during morning, orange during sunset, etc). I'm pretty new to Unity, so maybe my solution isn't the proper way to approach this.

    I've placed a game object with a simple plain white sprite on it, and then I'm trying to change the "color" property of the Sprite Renderer component with a script like this. Since I was still testing, I haven't change the color transparency yet:


    Code (CSharp):
    1.     public float duration;
    2.     private Color colorSunrise = new Color (88f, 165f, 209f);
    3.     private Color colorSunset = new Color (251f, 138f, 7f);
    4.     private float t = 0f;
    5.  
    6.     void Start()
    7.     {
    8.         GetComponent<SpriteRenderer> ().color = colorSunrise;
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.         Color color = Color.Lerp (colorSunrise, colorSunset, t);
    14.         t += Time.deltaTime / duration;
    15.         GetComponent<SpriteRenderer> ().color = color;
    16.     }
    With this code in theory I wanted to set the sprite color to the sunrise color once the game starts running, and then gradually change it to the sunset color. But when I run the game, it simply shows the original plain white sprite, and its color doesn't seem to be changed in the Inspector.

    However the funny thing is that if, while running, I click on the color property from the Inspector, I see that it is actually gradually changing its color perfectly, like shown in the screenshot attached (the inspector and the game are plain white, but the color is actually orange).

    ColorSprite.jpg

    I've been searching through the forums, Unity Answers, etc., and I understood that maybe the problem is the material. The sprite has the default material attached, so I tried to create a new material instead, but it didn't work neither. I tried something like:

    Code (CSharp):
    1. myMaterial = new Material(Shader.Find("Diffuse"));
    2.         myMaterial.color = colorSunrise;
    3.         GetComponent<SpriteRenderer> ().material = myMaterial;
    I tried changing the "Diffuse" property, but I didn't find any that worked, or maybe I'm simply doing it wrong. Since this is a 2D game without textures, lights, etc., I thought I don't need to work with materials, so I was looking for a simple solution, like just having a semi-transparent color layer above everything (like you would do on Photoshop), but I seems it wasn't that simple.

    Any help will be great appreciated, as well as any other approaches to solve this, in case I'm doing it wrong.

    Thank you very much.
     
  2. newsinanas

    newsinanas

    Joined:
    Oct 25, 2016
    Posts:
    2
  3. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    It's only been 1450 days.. :p:p
    Nice necro.
     
Thread Status:
Not open for further replies.