Search Unity

How can I change the opacity of a different object?

Discussion in '2D' started by MarcoE73, Apr 23, 2019.

  1. MarcoE73

    MarcoE73

    Joined:
    Jan 31, 2019
    Posts:
    12
    Hello, I'm creating a game that's about picking up trash in the ocean and I have a sprite that makes it seem like the ocean is "dirty", the problem is that the script im using is not set on this gameObject that I want to change the opacity of, so I have to access the game object and the sprite render. the thing is that I don't know how to access this game objects rendered and the color of it as well. I've tried a few different things but I can't seem to get it. So far this is the code that I have. I have it in the score because once the player collects the trash I want to change the opacity of the game object ("DirtyWaterEffect") of the little by little. Any help is much appreciated because I cannot figure this out, thank you.

    Code (CSharp):
    1. [code=CSharp]public class TrashScore : MonoBehaviour
    2. {
    3.     public Text scoreText;
    4.     public Sprite dirtyWater; //the sprite that makes it seem like the ocean is dirty
    5.  
    6.  
    7.     void Start()
    8.     {
    9.  
    10.         dirtyWater = GameObject.FindGameObjectWithTag("DirtytWaterEffect").GetComponent<SpriteRenderer>().sprite;
    11.  
    12.     }
    13.  
    14.     private void Awake()
    15.     {
    16.         scoreText.text = "Trash Collected: " + GameControl.control.score.ToString();
    17.     }
    18.  
    19.     private void OnTriggerEnter2D(Collider2D collision)
    20.     {
    21.         if (collision.gameObject.tag == "Player")
    22.         {
    23.             GameControl.control.score++;
    24.             Destroy(this.gameObject);
    25.  
    26.             scoreText.text = "Trash Collected: " + GameControl.control.score.ToString();
    27.  
    28.          
    29.         }
    30.     }
    31.  
    32.  
    33. }
    34.  
    [/code]
     
  2. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    Code (CSharp):
    1. dirtyWater = GameObject.FindGameObjectWithTag("DirtytWaterEffect").GetComponent<SpriteRenderer>().color;
    its just .color instead of .sprite. the sprite render has the ability to tint your sprite without editing the sprite
     
  3. MarcoE73

    MarcoE73

    Joined:
    Jan 31, 2019
    Posts:
    12

    hey I get an error saying I cannot convert 'UnityEngine.Color' to UnityEngine.Sprite'
     
  4. GazingUp

    GazingUp

    Joined:
    Jun 16, 2015
    Posts:
    271
    Look at the dirtyWater variable's type. Unity is complaining because you're assigning a color type to a sprite type variable.

    Put public Color dirtyWater and see
     
  5. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    A sprite is an asset that you assign to a SpriteRenderer to display it. You want to be using SpriteRenderer to change the tint of the sprite, including opacity.

    Could you make the "dirtyWater" variable a SpriteRenderer type, and drag the dirtyWater object to that field in the inspector for "TrashScore" to avoid needing to do FindObject and GetComponent on Awake altogether?

    Then you can use "dirtyWater.color" to set the color, including opacity.

    Here's an example of what that might look like:
    Code (CSharp):
    1. public class TrashScore : MonoBehaviour
    2. {
    3.     public Text scoreText;
    4.  
    5.     // drag the dirty water object here in the inspector
    6.     public SpriteRenderer dirtyWater;
    7.  
    8.     // use this function to change the water's opacity
    9.     private void SetWaterOpacity(float opacity)
    10.     {
    11.         Color newColor = dirtyWater.color;
    12.         newColor.a = opacity;
    13.         dirtyWater.color = newColor;
    14.     }
    15.  
    16.     private void OnTriggerEnter2D(Collider2D collision)
    17.     {
    18.         if (collision.gameObject.tag == "Player")
    19.         {
    20.             GameControl.control.score++;
    21.             Destroy(this.gameObject);
    22.  
    23.             scoreText.text = "Trash Collected: " + GameControl.control.score.ToString();
    24.         }
    25.     }
    26. }
     
  6. MarcoE73

    MarcoE73

    Joined:
    Jan 31, 2019
    Posts:
    12
    hey it worked, thanks! do you think you can help me see why the alphalevel isn't decreasing when I pick up the trash?

    Code (CSharp):
    1. public Text scoreText;
    2.     public Color dirtyWater;
    3.  
    4.     public float decreaseAlpha = .5f; //what I want the alpha level to decrease by
    5.     public float alphaLevel = 1f; //the original alphaLevel
    6.  
    7.  
    8.     void Start()
    9.     {
    10.         dirtyWater = GameObject.FindGameObjectWithTag("DirtyWaterEffect").GetComponent<SpriteRenderer>().material.color;
    11.     }
    12.     void Update()
    13.     {
    14.         dirtyWater = new Color(1, 1, 1, alphaLevel); //the new color of the sprite and it should update once the player collides with the trash
    15.     }
    16.  
    17.     private void Awake()
    18.     {
    19.         scoreText.text = "Trash Collected: " + GameControl.control.score.ToString();
    20.     }
    21.  
    22.     void OnTriggerEnter2D(Collider2D collision)
    23.     {
    24.         if (collision.gameObject.tag == "Player")
    25.         {
    26.             GameControl.control.score++;
    27.             Destroy(this.gameObject);
    28.  
    29.             scoreText.text = "Trash Collected: " + GameControl.control.score.ToString();
    30.  
    31.             alphaLevel -= decreaseAlpha; //im decreasing the alphaLevel here when you pick up the trash
    32.            
    33.         }
    34.     }
     
  7. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    public Color dirtyWater is you saving a color... when you use alphaLevel -= decreaseAlpha;
    then apply it in update dirtyWater = new Color(1, 1, 1, alphaLevel);
    all you are doing us updating that local color. you need to actually apply it to the material

    material.color = new Color(1, 1, 1, alphaLevel);

    (material should be a variable you save)
    instead of this dirtyWater = GameObject.FindGameObjectWithTag("DirtyWaterEffect").GetComponent<SpriteRenderer>().material.color;

    do
    Material mat = GameObject.FindGameObjectWithTag("DirtyWaterEffect").GetComponent<SpriteRenderer>().material;
    in start or awake
    then later you can do
    mat.color = new Color(1, 1, 1, alphaLevel);
     
  8. GazingUp

    GazingUp

    Joined:
    Jun 16, 2015
    Posts:
    271
    Like the others said, you don't see a change in alpha because your variable dirtyWater, is just a Color. It has no knowledge about the sprite material you are changing. So you want to have dirtyWater be a variable of the sprite material itself, rather than type Color. Then you can access the Color of said material, and do the same thing you're doing.

    So if you do public Material dirtyWater, and then put dirtyWater.color = new Color(1, 1, 1, alphaLevel);

    Try that and see.
     
  9. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Last edited: Apr 25, 2019
    GazingUp likes this.
  10. MarcoE73

    MarcoE73

    Joined:
    Jan 31, 2019
    Posts:
    12

    thank you I figured it out by using your example, I really appreciate the help!!!
     
    LiterallyJeff likes this.