- Home /
How to make a Transparent GameObject?
I have this script for making the material transparent. Is it right? Also it gives me this error: Assets/MyGame/Transparent.cs(6,46): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration using UnityEngine; using System.Collections;
public class Transparent : MonoBehaviour
{
gameObject.renderer.material.color.a = 1;
}
Idk whats wrong with it though. Any answer helps :D
Answer by Jeff-Kesselman · May 16, 2014 at 11:51 PM
Um.
Lots.
FIRST off, you can't just have code sitting in a C# class. It needs to be in a method. In this case, probably the Start method
public class Transparent : MonoBehaviour
{
void Start(){
gameObject.renderer.material.color.a = 1;
}
}
Secondly, you probably can't set the color that way. Color is likely a struct which means you probably have to set gameObject.renderer.material.color to a complete color.
public class Transparent : MonoBehaviour
{
void Start(){
gameObject.renderer.material.color=
new Color(1.0f,1.0f.1.0f,1.0f);
}
}
THIRD, alpha of 1.0 is fully opaque, you want something less then 1 if you want it to be translucent.
FOURTH, it wont work anyway unless you have a transparent renderer set.
Ok thanks, well now its giving me this: Assets/$$anonymous$$yGame/Trans.cs(11,53): error CS1526: A new expression requires () or [] after type
If you you can, how would i add a transparent renderer would it just be
gameObject.renderer.transparent.material.color=
?
You'll need to apply a transparent diffuse shader to your object too in order for transparency to work.
Im completely new to Unity, so i have no idea on how to do this :/
Create a new material and choose the Transparent Diffuse shader from the shader dropdown box. Then drag it onto your object in the scene.
Use @Jeff-$$anonymous$$esselman example to change the alpha by code, or you can do it in the inspector.
https://docs.unity3d.com/Documentation/Components/shader-TransDiffuse.html
Answer by Gords · May 24, 2016 at 02:15 PM
i dont know if im missing something, or maybe is a new feature in unity 5 but i just selected "Rendering Mode: Transparent" inside my material, then assigned it to my gameObject and, thats it.
This is pre "physically based rendering". Setting the shaders has changed a bit yes, but the rest should still apply.
I think it is changed in Unity 5. Transparent Diffuse: "Unity 5 introduced the Standard Shader which replaces this shader."
I did this and it worked too. What I neglected to do at first was to change the alpha of the color after setting this.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Using Multiple Tiles for Textures 0 Answers
Audio Fades 1 Answer
UnityEngine.Input.GetMouseButton(1)) issue 1 Answer
Primitive block texturing (In code) HELP 0 Answers