SpriteRenderer.color doesn't work - bug?

Hi,

i’m a new to unity and trying to change a sprite’s color through a script.
i’m following this tutorial page: Unity - Coroutines
i’m using Unity 2019.2.0f1

this is the script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class colorChanger : MonoBehaviour
{
   
    public int                 seconds_interval;
    public SpriteRenderer     sr;
    public Color             color_1;
    public Color             color_2;
   
    IEnumerator changeColor(){
       
        while (true){
           
            if (sr.color == color_1){
                sr.color = color_2;
            } else {
                sr.color = color_1;
            }
           
            yield return new WaitForSeconds(seconds_interval);
        }
       
    }
   
    // Start is called before the first frame update
    void Start()
    {
        sr = gameObject.GetComponent<SpriteRenderer>();
        StartCoroutine( changeColor() );
        //sr.color = color_1;
    }

    // Update is called once per frame
    void Update()
    {
       
    }
}

this is the sprite, notice it starts as purple, then the script should change it’s color:



when pressing “play” i can see the colors changing every second in the inspector, but nothing happens in the scene:
yellow:


green (after 1 second):


actually, the inspector is totaly ignored by the scene! notice that the sprite is white (and not purple/yellow/green).
notice that if i change the scale nothing happens in the “play” screen:


but when i go back to “game” screen (without stopping the game), the frame of the sprite changes but not it’s actual size:


it looks like the script is making the inspector disconnect from the scene.
is it a bug? or am i missing something here?

thank you :slight_smile:

Your problem is here. You need to change the colour but your code does not access the Color property.

4818161--461654--Capture.JPG

I don’t understand, I think I do access the color property. here:

sr.color = color_2;

also, i can see the colors changing in the inspector every second from yellow to green, but nothing happens in the scene.

what should i change?

You need to access the color in the line I quoted in my previous post.

solved!

Thank you APSchmidt you ansipred me to check the actuall assignments! (i am new to unity so for me it’s not trivial)

my problem was that: I have chosen colors wile their Alpha property was 0.
so i couldn’t see any color changing.

1 Like

I ran into the same problem. Thanks urkidoo!