How to make a game object transparent in unity

In my game, I want to make the object transparent for 2 seconds by writing a script at runtime if the player encounters a certain object during the game ... is this possible?

+6
source share
4 answers

Check for collision. When the collision that you want occurs, 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 this to have your program wait time: http://docs.unity3d.com/Documentation/Manual/Coroutines.html

You will notice that an example is just your question.

+10
source

Try using 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); } 

Then you can call it:

 gameObject.renderer.material.ChangeAlpha( Your Alpha Value ); 
+4
source

The object you are calling must have a shader that supports transparency. In Unity5, when using the standard shader, you must explicitly set it to Transparent in order to be able to manipulate the alpha value. enter image description here

enter the link here

It should also be clear that the alpha value is a float that goes from 0.0f to 1.0f, so for example setting

 var other : GameObject; other.renderer.material.color.a = 0.5; // 50 % transparent other.renderer.material.color.a = 1.0; // 100% visible 

will make the object transparent to 50%.

+1
source

in Unity 5, the best way that worked for me (TO MAKE OBJECT INVISIBLE) was to set all the materials of the game object that I would like to be invisible for transparency in rendering mode. Then click on the small round button next to the albedo, and then scroll through the list of items until you find one of them, called UIMask. Highlight it and press enter. I'm a newbie, so ask if you need more clarification.

* Please note that this is a hard fix, and I'm not sure if you can change this with code.

** This was done for borders in roll-a-ball with the player jump function enabled. it was necessary to make the walls invisible, but also collisions that could stop an object born in the air,

0
source

Source: https://habr.com/ru/post/956386/


All Articles