Search Unity

How do you change a color in SpriteRenderer?

Discussion in '2D' started by Ilingis, Nov 14, 2013.

  1. Ilingis

    Ilingis

    Joined:
    Dec 13, 2012
    Posts:
    63
    How do you change a color in Spriterenderer with script? Thanks in advance.
     
  2. jquave

    jquave

    Joined:
    Oct 3, 2009
    Posts:
    21
    renderer.material.SetColor("_Color", Color.red);
     
    GabiBulat likes this.
  3. XGundam05

    XGundam05

    Joined:
    Mar 29, 2012
    Posts:
    473
    Or rather, you can probably just use SpriteRenderer.color
    Code (csharp):
    1.  
    2. SpriteRenderer renderer = //Get the renderer via GetComponent or have it cached previously
    3. renderer.color = new Color(0f, 0f, 0f, 1f); // Set to opaque black
    4.  
     
    Demulc, asingh99222, CJ_Smith and 2 others like this.
  4. Ilingis

    Ilingis

    Joined:
    Dec 13, 2012
    Posts:
    63
    Thanks! I added this line renderer = gameObject.GetComponent(SpriteRenderer); into the Start function and now it works fine! You saved me some headache, XGundam05.

    jquave: Thanks to you, as well.
     
  5. c0d3n4m3

    c0d3n4m3

    Joined:
    Sep 21, 2014
    Posts:
    2
    This will apply a Material Shader Tint colour to your sprite, this doesn't alter the SpriteRenderer colour. Still handy to know though! :)
     
    Dzianis likes this.
  6. RhinoFreak

    RhinoFreak

    Joined:
    Dec 11, 2017
    Posts:
    6
    How do I change the SpriteRenderer color then?
     
  7. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807