'Change the opacity of gameobject Unity and Change the look of a sprite
I am creating a unity game, where I have different tiles, (2D) and I was wondering two things. One, how do you make a sprite more transparent. And two, how would I change the look of a gameObject. so say we have a dirt block and I want to change it to a stone block. How would do that?
Thanks!
Edit: Using code
Solution 1:[1]
I'm assuming you want to do all of this in Code, because you can't change a sprite during runtime inside of the Unity Editor.
To make a Sprite more transparent you have to access the color value of your SpriteRenderer.
SpriteRenderer.color = new Color(1f, 1f, 1f, 1f);
This is an example of how it could look. The first three numbers are responsible for the color of the object (R, G, B). The fourth color changes the opacity of your object. This is how it would look with only half the opacity.
SpriteRenderer.color = new Color(1f, 1f, 1f, 0.5f);
Now to your second question on how to change a texture at runtime.
There are different ways you can go about this.
gameObject.GetComponent<SpriteRenderer> ().sprite = Resources.Load("img1", typeof(Sprite)) as Sprite;
or
public Sprite stoneSprite;
SpriteRenderer.sprite = stoneSprite;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | GetMyIsland |




