What I’m trying to do is select a single object of a parent and fade the rest to a specified transparent value. I’m using recursion to iterate through all the children of the parent and set all that aren’t selected to a fade_material and those that are selected to an opaque_material.
I have searched this topic for days and have not found a working solution.
I’ve tried using Renderer.material and Renderer.sharedMaterial.
Here’s my recursive code that seems to make instances of materials vs just changing the material:
private void ChangeMaterial(GameObject parent, bool excluded)
{
foreach (Transform child in parent.transform)
{
// Check if child is a parent to other children
if(child.childCount > 0)
{
// More children… check if parent node is selected
if(excluded || (child.gameObject == selectedObject))
{
ChangeMaterial(child.gameObject, true);
}
else
{
ChangeMaterial(child.gameObject, false);
}
}
else
{
if(excluded || (child.gameObject == selectedObject) || (transparencySlider.value == transparencySlider.maxValue))
{
// Opaque
Renderer rend = child.GetComponent();
rend.material = child.GetComponent().opaqueMaterials[0];
}
else
{
// Fade
Renderer rend = child.GetComponent();
rend.material = child.GetComponent().fadeMaterials[0];
}
}
}
}
Does the loop used have any affect over this issue?
Thank you for your help…