- Home /
How to make an object transparent in script?
Hello! In the game I'm making I have it where you can zoom the camera in and out which can result in the camera clipping through objects. I was wondering how could I make it that the object becomes transparent (or semi-transparent) once the camera gets close enough? I'm having trouble figuring out how to change transparency in a script.
Answer by tomicz · Oct 11, 2020 at 01:59 AM
You can calculate the distance between the camera and the object that you want to make transparent. When the distance is very close then make the object transparent. You have to attach a material that supports transparency to the object you want to make transparent. The materials are made transparent when the camera is too close.
I don't know your code because you posted none, but that is the most basic way to do it.
Answer by LilTrumpet · Oct 11, 2020 at 02:34 AM
That is what I figured would have to be done. But I do not know how to change the transparency of an object in a script. I'm extremely new to Unity and I haven't been able to find anything that really explains it for me. Many posts I found were either old or they changed the transparency of the material which meant all objects with that material became transparent when I only want the object in front of the camera to become transparent.
Answer by CodesCove · Oct 11, 2020 at 05:25 PM
If you get the reference to the target objects Renderer and set its material.color then it will work without affecting any Material asset colors (or other objects). Like this (pseudo code):
Renderer renderer = [target game object].GetComponent<Renderer>();
renderer.material.color = Color.clear; //this will immediately change the color to the transparent.. and only for that specific object
Changing gradually colors "a" component (alpha component) towards 0 will produce fade effect (and vice versa). alpha has value from 1 to 0 (non transparent (opaque) to transparent).
You can use Mathf.Lerp function to interpolate the suitable alpha value btw 1 and 0. For example renderer.material.color.a = Mathf.Lerp(minDistance, maxDistance, actualDistance)
There is also Color.Lerp method to produce direct color transformations https://docs.unity3d.com/ScriptReference/Color.Lerp.html
Hope this helps or at least points you to the right direction
I searched some more from answers and I stumbled upon this post. link For now I've just copied their code to see if it works then I would go back and study it so I can try to better understand it. However, before I am able to test it I'm getting two compiler errors that are related to the same thing. The errors I am getting are both "CS0411: The type arguments for method 'Component.GetComponent()' cannot be inferred from the usage. Try specifying the type arguments explicitly." (I would attach screenshots but for some reason I am unable to.) This may be a simple issue but I am very new to Unity and have a headache so I'm unable to focus on this right now. If you could tell me how to fix this it would be amazing.