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

Any way to programmatically change SpriteRenderer color, not the Sprites-Default tint?

Discussion in '2D' started by gtzpower, Sep 5, 2014.

  1. gtzpower

    gtzpower

    Joined:
    Jan 23, 2011
    Posts:
    318
    Hi everyone, I would like to change the SpriteRenderers color at runtime via code OR I would be happy with changing the materials tint in the editor. My main goal is to have a color field that I can set in the editor manually and via code, but the problem is that if I set the color of the sprite renderer to gray in the editor, then I set the material color (aka the Tint), end up getting my desired color multiplied by grey. I would be fine with setting the SpriteRenderers color to white and the tint to my default color if I could do so in the editor, but the material is only accessible at runtime it seems. On the other hand, the SpriteRenderer color only seems to be accessible in the editor.

    Unfortunately, my only thought is to set all renderer colors to white in the editor, and programmatically apply tint's at runtime to get the precise colors I want. This makes editing difficult though when everything is the same color :(

    Any thoughts?
     
  2. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    If I am interpreting your question correctly, you just want to be able to programatically change the color of a sprite using its SpriteRenderer.
    The following code should be able to accomplish that.
    SpriteRenderers have a property called color that can be accessed and changed at runtime.

    Code (CSharp):
    1. (SpriteRenderer)GetComponent<SpriteRenderer>().color = Color.green;
     
  3. gtzpower

    gtzpower

    Joined:
    Jan 23, 2011
    Posts:
    318
    Thank you Limeoats! I am not sure how I missed this, but I thought for sure I tried it. I do notice that the color in the inspector isn't updating until I deselect the object and reselect it, so maybe that was throwing me off the path. But alas, your solution works. Thank you!