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.

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.

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.

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 Unity - Scripting API: Color.Lerp

Hope this helps or at least points you to the right direction