I basically just need to change an object’s transparency while the game is running. Is there a transparency variable that I can access in order to this? And is it a problem if multiple objects use the same material but I only want one of their transparency to change? I searched to docs and forums and can’t seem to find anything that works. Thanks in advance for any help!
if you assign a material with color you can change the alpha on that color.
Maybe this is what you are looking for ?
var other : GameObject;
other.renderer.material.color.a = 0.5; // 50 % transparent
other.renderer.material.color.a = 1.0; // fully opaque
In Unity 2.6 Pro:
You must create this shader:
[Link Removed, No Longer Valid] and link him to all materials in object.
Than you can change the transparency:
Component[] renderers = GameObject.GetComponentsInChildren(typeof(Renderer));
foreach (Renderer curRenderer in renderers)
{
Color color;
foreach (Material material in curRenderer.materials)
{
color = material.color;
// change alfa for transparency
color.a -= 0.01f;
if (color.a < 0)
{
color.a = 0;
}
material.color = color;
}
}
Here try something like this:
private float duration = 1.0f;
// Update is called once per frame
void Update () {
Color textureColor = renderer.material.color;
textureColor.a = Mathf.PingPong(Time.time, duration) / duration;
renderer.material.color = textureColor;
}
Everyone’s suggestions here are good.
If you are still having problems though…
-
Make sure that you are using a shader that supports Transparency…in Unity 3.x, use any shader under the “Transparent” group.
-
If you are using something like tk2d or another external tool, you may need to access a different component than the renderer to change the color. For example, in tk2d, you have to access the component “Tk2dSprite” and then use the color property of that component to change the color. It could be a similar deal with other tools like SpriteManager.
Hi…
I use the following code in my games. But, in this case, the object will disappear in some interval
float Delay;
Color colorBegin;
Color colorEnd;
....
void Awake()
{
colorBegin = GetComponent<SpriteRenderer>().color;
colorEnd = new Color(colorBegin.r, colorBegin.g, colorBegin.b, 0f);
}
void Update()
{
if (tag == TAGSUPPLYFAID || tag == TAGSUPPLYENERGY)
{
Delay += Time.deltaTime;
GetComponent<SpriteRenderer>().color = Color.Lerp(colorBegin, colorEnd, Delay * 0.1f);
if (transform.GetComponent<SpriteRenderer>().color.a==0)
{
GameObject.Destroy(gameObject);
}
}
}