C# Script - Make wall to become partially transparent

Hi everyone. i am trying to create my first game, and i want to make my walls transparent, when the player is behind them.
I’ve found this code:

using UnityEngine;
using System.Collections.Generic;

public class HideWalls : MonoBehaviour
{
//The player to shoot the ray at
public Transform player;
//The camera to shoot the ray from
public Transform camera;

//List of all objects that we have hidden.
public List hiddenObjects;

//Layers to hide
public LayerMask layerMask;

private void Start()
{
//Initialize the list
hiddenObjects = new List();
}

void Update()
{
//Find the direction from the camera to the player
Vector3 direction = player.position - camera.position;

//The magnitude of the direction is the distance of the ray
float distance = direction.magnitude;

//Raycast and store all hit objects in an array. Also include the layermaks so we only hit the layers we have specified
RaycastHit[ ] hits = Physics.RaycastAll(camera.position, direction, distance, layerMask);

//Go through the objects
for (int i = 0; i < hits.Length; i++)
{
Transform currentHit = hits*.transform;*

  • //Only do something if the object is not already in the list*

  • if (!hiddenObjects.Contains(currentHit))*

  • {*

  • //Add to list and disable renderer*

  • hiddenObjects.Add(currentHit);*

  • currentHit.renderer.enabled = false;*

  • }*

  • }*

  • //clean the list of objects that are in the list but not currently hit.*

  • for (int i = 0; i < hiddenObjects.Count; i++)*

  • {*

  • bool isHit = false;*

  • //Check every object in the list against every hit*

  • for (int j = 0; j < hits.Length; j++)*

  • {*
    _ if (hits[j].transform == hiddenObjects*)_
    _
    {_
    _
    isHit = true;_
    _
    break;_
    _
    }_
    _
    }*_

* //If it is not among the hits*
* if (!isHit)*
* {*
* //Enable renderer, remove from list, and decrement the counter because the list is one smaller now*
_ Transform wasHidden = hiddenObjects*;
wasHidden.renderer.enabled = true;
hiddenObjects.RemoveAt(i);
i–;
}
}
}
}*
And i need to know, if it’s possible to make it partially transparent, instead of fully disappear…as i said, i am new in Unity and scripting, and i appreciate your help. Thank you so much._

Make the wall material Transparent / diffuse

you need to change these 2 lines

currentHit.renderer.enabled = false;
currentHit.renderer.enabled = true;

to

currentHit.renderer.material.color.a =0.25f; // Change for varying opacity
currentHit.renderer.material.color.a =1f; // Reset to full solidness

I think that will work

that makes these walls fully disappear.
edit: i’ve already tried this solution, but then, i will get this error:

““Cannot modify a value type return value of `UnityEngine.Material.color’. Consider storing the value in a temporary variable””

How do you know it makes walls fully disappear if when you tried it you got errors?

Try this to remove the error

Color newColor = new Color(1, 1, 1, 0.25f);
currentHit.renderer.material.color.a = newColor.a;

I have errors, only, when i change

currentHit.renderer.enabled = false;
currentHit.renderer.enabled = true;

To this:

currentHit.renderer.material.color.a =0.25f;
currentHit.renderer.material.color.a =1f;

edit: Nope…still getting error, even after update.

remove the .a

    Color newColor = new Color(1, 1, 1, 0.25f);
    currentHit.renderer.material.color = newColor;

Thanks…!!! this solution works. Now the walls turns to gray (permanently), when i come close to then.

edit: Even when i change values…the wall is still gray, and it stays gray, even when i am not behind it.

ok…edit2 : Colors are changing, but i want to change transparency of the material and not the colors.

…At least i would like to setup to hide only one object (not everything)
for example: Wall | Shelf | Player
and that it would hide only that wall

Try this:

Color newColor = currentHit.renderer.material.color;
newColor.a = 0.25f;
 currentHit.renderer.material.color = newColor;