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]public class TrashScore : MonoBehaviour
{
public Text scoreText;
public Sprite dirtyWater; //the sprite that makes it seem like the ocean is dirty
void Start()
{
dirtyWater = GameObject.FindGameObjectWithTag("DirtytWaterEffect").GetComponent<SpriteRenderer>().sprite;
}
private void Awake()
{
scoreText.text = "Trash Collected: " + GameControl.control.score.ToString();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
GameControl.control.score++;
Destroy(this.gameObject);
scoreText.text = "Trash Collected: " + GameControl.control.score.ToString();
}
}
}
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:
public class TrashScore : MonoBehaviour
{
public Text scoreText;
// drag the dirty water object here in the inspector
public SpriteRenderer dirtyWater;
// use this function to change the water's opacity
private void SetWaterOpacity(float opacity)
{
Color newColor = dirtyWater.color;
newColor.a = opacity;
dirtyWater.color = newColor;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
GameControl.control.score++;
Destroy(this.gameObject);
scoreText.text = "Trash Collected: " + GameControl.control.score.ToString();
}
}
}
hey it worked, thanks! do you think you can help me see why the alphalevel isn’t decreasing when I pick up the trash?
public Text scoreText;
public Color dirtyWater;
public float decreaseAlpha = .5f; //what I want the alpha level to decrease by
public float alphaLevel = 1f; //the original alphaLevel
void Start()
{
dirtyWater = GameObject.FindGameObjectWithTag("DirtyWaterEffect").GetComponent<SpriteRenderer>().material.color;
}
void Update()
{
dirtyWater = new Color(1, 1, 1, alphaLevel); //the new color of the sprite and it should update once the player collides with the trash
}
private void Awake()
{
scoreText.text = "Trash Collected: " + GameControl.control.score.ToString();
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
GameControl.control.score++;
Destroy(this.gameObject);
scoreText.text = "Trash Collected: " + GameControl.control.score.ToString();
alphaLevel -= decreaseAlpha; //im decreasing the alphaLevel here when you pick up the trash
}
}
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 should be a variable you save)
instead of this dirtyWater = GameObject.FindGameObjectWithTag(“DirtyWaterEffect”).GetComponent().material.color;
do
Material mat = GameObject.FindGameObjectWithTag(“DirtyWaterEffect”).GetComponent().material;
in start or awake
then later you can do
mat.color = new Color(1, 1, 1, alphaLevel);
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);
You don’t want to be editing the material directly at runtime, it’s a persistent asset and will save your changes between plays in the editor unless you’re operating on a material instance. Use the SpriteRenderer type which has a Color property.