Changing material type in a URP Lit material does not take effect until material is viewed in editor
So I'm making a project which uses Universal Render Pipeline (URP), and I'm trying to make a 'corpse decay' effect which causes corpses created by dead characters to fade out after a second. I'm doing this by changing the alpha of the Mesh Renderers' materials through a script. I'm pretty sure the script works perfectly as the alpha does change when it should, and the object gets disabled when it reaches 0, but the problem is that the change in alpha doesn't render until I view the material in the inspector. Here is the script I am using:
[SerializeField] [Tooltip("Delay before decay begins")] float delay = 1;
[SerializeField] [Tooltip("Time for corpse to fully decay")] float period = 1;
List<MeshRenderer> renderers = new List<MeshRenderer>();
float timer;
private void Awake()
{
foreach(MeshRenderer mesh in GetComponentsInChildren<MeshRenderer>())
{
mesh.material.SetFloat("_Surface", 1);
renderers.Add(mesh);
}
}
private void Update()
{
if(timer >= delay)
{
foreach (MeshRenderer mesh in renderers.ToArray()) // For all mesh renderers
{
Color matColor = mesh.material.color; // Gets material color
matColor.a = Mathf.MoveTowards(matColor.a, 0, (1 / period) * Time.deltaTime); // Decreases alpha
mesh.material.SetColor("_BaseColor", matColor); // Sets color of material
if (matColor.a <= 0) // If material has reached 0 alpha
{
renderers.Remove(mesh); // Removes this material from the list
}
}
}
else
{
timer += Time.deltaTime;
}
if (renderers.Count == 0)
{
gameObject.SetActive(false);
}
}
I think the problem is that I'm changing the material's surface type from 'Opaque' to 'Transparent' through the script, but if I don't do that, changing the alpha doesn't even make the material fade. I just don't see why it only works when viewed in the inspector. Is there any other way I can do this fade
Your answer
Follow this Question
Related Questions
How can i make a tiled/striped texture on a sphere ? (see example below) 0 Answers
Change alpha on material with custom shader 0 Answers
Check if emission is enabled in URP Lit Shader from C# script 0 Answers
Highlighting Multiple Objects with an Outline Shader 0 Answers
Accessing shader colors 1 Answer