Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Unity 4.3] How to change the opacity of a 2D sprite ?

Discussion in '2D' started by BenoitFreslon, Jan 17, 2014.

  1. BenoitFreslon

    BenoitFreslon

    Joined:
    Jan 16, 2013
    Posts:
    163
    Hello,

    I'm new with unity.

    But I don't know to how to change the opacity of a 2D sprite.

    Thanks.
     
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    Change the color.
    Color(R,G,B,A). A is the alpha.

    So:
    SpriteRenderer.color = new Color(1f,1f,1f,1f) is a normal sprite
    SpriteRenderer.color = new Color(1f,1f,1f,.5f) is about 50% transparent
    SpriteRenderer.color = new Color(1f,1f,1f,0f) is about 100% transparent (Cant be seen at all, but still active)
     
  3. DrMellieMel

    DrMellieMel

    Joined:
    Oct 27, 2014
    Posts:
    1
    Thank you!
     
  4. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    Thanks a lot for this.
     
  5. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,546
    Hrm...

    I'm trying the same thing, but placing a float in the location and getting an error:
    Code (csharp):
    1.  
    2. public class Darkness : MonoBehaviour {
    3.  
    4.     float alphaLevel;
    5.     float alphaFactor;
    6.     public float totallyDarkHeight = 1000f;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         alphaFactor = 1 / totallyDarkHeight;
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void FixedUpdate () {
    15.         alphaLevel = alphaFactor * transform.position.y;
    16.         if (alphaLevel > 1){
    17.             alphaLevel = 1;
    18.         }
    19.         SpriteRenderer.color = new Color (1f, 1f, 1f, alphaLevel);
    20.     }
    21. }
    22.  
    It's supposed to becoming more opaque as the object goes higher.
    But when I try it, it give back this:

    "An object reference is required to access non-static member"

    Am I just screwing up the C# code?
    I've been switching to it from JS.

    EDIT: Shouldn't it read:
    Code (csharp):
    1.  
    2.         gameObject.SpriteRenderer.color = new Color (1f, 1f, 1f, alphaLevel);
    3.  
    ???

    And why do I get the same error when I do?
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You need to use GetComponent. Also, FixedUpdate is only for physics; use Update.

    --Eric
     
    Not_Sure likes this.
  7. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,546
    Ah man, why am I struggling so much...

    Thank you Eric!
     
  8. noodlesdev

    noodlesdev

    Joined:
    Jun 16, 2015
    Posts:
    5
    Finally! this worked
     
  9. Shadoninja

    Shadoninja

    Joined:
    Nov 12, 2013
    Posts:
    26
  10. lai32290

    lai32290

    Joined:
    Apr 2, 2016
    Posts:
    6
    Thank you so much!
     
  11. lai32290

    lai32290

    Joined:
    Apr 2, 2016
    Posts:
    6
    Is able to set it without code?
     
  12. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    Yes, in the color selector on the sprite in the inspector. There are four sliders, the last one is the alpha.
     
    inabird likes this.
  13. MrDinmaker

    MrDinmaker

    Joined:
    Sep 19, 2019
    Posts:
    1
    This is gonna be an odd addition to this question, but is it possible to make parts of a sprite transparent without affecting the rest? As if I were dragging an x-ray lens over part of the sprite so that whatever was under the lens would be transparent but the rest was still opaque?
     
  14. YellowGD

    YellowGD

    Joined:
    Aug 21, 2017
    Posts:
    1
    Thank you so much!
     
  15. PoolloverNathan

    PoolloverNathan

    Joined:
    Sep 30, 2020
    Posts:
    1
    Sorry for necroing, but you can use Sprite Masks to do this. Set
    Mask Interaction
    on the sprite to
    Visible Outside Mask
    to make the mask apply to your sprite.
     
    electrictuts likes this.
  16. MrNarveL

    MrNarveL

    Joined:
    Nov 10, 2020
    Posts:
    2
    upload_2022-9-3_6-45-52.png

    You can do it from here, by clicking on the color in Sprite Renderer, Leave the RGB at 255 and change the alpha from 0 (Invisible) to 255 (completely visible)