In my game I want to make the player object transparent for 2 seconds by scripting at run time if the player collided with a specific object during the game ... is it possible ?
-
Accepted answer is in [tag:C#] so I'm adding it as a tag. – Ruzihm Jun 28 '22 at 20:23
4 Answers
Check for collision. When the collision that you want is triggered then you can change the transparency.
GameObject g;
// 50% Transparency.
g.renderer.material.color.a = 0.5f; // a is the alpha value.
// 100% Transparency.
g.renderer.material.color.a = 1.0f;
You can do just this to make your program wait time: http://docs.unity3d.com/Documentation/Manual/Coroutines.html
You will notice the example is exactly your question.
-
7Unity doesn't allow you to modify value of Color.a. you must use a temporary variable. – Mostafa Feb 20 '16 at 14:08
-
Try this extension method:
public static void ChangeAlpha(this Material mat, float alphaValue)
{
Color oldColor = mat.color;
Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, alphaValue);
mat.SetColor("_Color", newColor);
}
You can then call it by:
gameObject.renderer.material.ChangeAlpha( Your Alpha Value );

- 1
- 1

- 1,615
- 3
- 18
- 32
In Unity 5, the best way (TO MAKE AN OBJECT INVISIBLE) that worked for me was to:
- Set all of the game object's materials you want to be invisible to transparent under the rendering mode.
- Then, click on the small round button next to albedo and scroll down on the list of items given until you find one called
UIMask
. - Highlight it and press enter.
* Note that this is a hard fix and I'm not sure if you can change this with code.
** This was made for boundaries in roll-a-ball with a player jump function included. I needed to make the walls invisible but also collision-able to stop the air born player object.

- 7,102
- 69
- 48
- 77
-
1that can mess up if other objects use that object, its best to create a new shader to do this and turn off all of the extras to the mesh renderer. – 1-14x0r Nov 10 '20 at 01:09
This can be simply done with shaders, which you can quickly and efficiently change at runtime:
- Create new material and set the shader to use the
Unlit/Transparent Cutout
- Assign your game object to use this new material
- Turn off all of the stuff in the mesh render of the game object: set cast shadows to off, receive shadows disabled, contribute to global illumination to disabled, turn off your light probes and reflections, set motion vectors to 'force no motion', turn off dynamic occlusion.
- Also don't forget to remove the mesh collider of the game object.

- 7,102
- 69
- 48
- 77

- 1,697
- 1
- 16
- 19