Hello there! Im starting off with Unity and working on a VR passion project, learning little by little.
I’ve made a scene with full VR working and started placing 3d models onto scene, one object I need to add a transparency slider for.
By following YouTube tutorial I was able to put together a script to attach slider value to alpha of the material, but it does not seems to be doing anything in game.
I’m using URP, object material is set to Transparent. With said script I’ve added my object and referenced it in the slider On-Value changed.
Would appreciate a helping hand! Thanks in advance!
using UnityEngine;
using UnityEngine.UI;
public class MakeObjectTransparent : MonoBehaviour
{
public GameObject currentGameObject;
public float alpha = 0.5f;//half transparency
//Get current material
private Material currentMat;
// Start is called before the first frame update
void Start()
{
currentGameObject = gameObject;
currentMat = currentGameObject.GetComponent<Renderer>().material;
}
// Update is called once per frame
void Update()
{
//ChangeAlpha(currentMat, alpha);
}
void ChangeAlpha(Material mat, float alphaVal)
{
Color oldColor = mat.color;
Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, alphaVal);
mat.SetColor("_Color", newColor);
}
public void ChangeAlphaOnValueChange(Slider slider)
{
ChangeAlpha(currentMat, slider.value);
}
}