I want to change all of materials on a game object’s child objects to a simple silhouette material, and then change them back to the old ones when user inputs a certain command.
My functions are as below:
public List<Material> mats;
public Material SilhouetteMaterial;
public void AttachSilhouette()
{
mats = new List<Material>();
var childrenRenderers = objects.GetComponentsInChildren<Renderer>();
foreach (var childRenderer in childrenRenderers)
{
//Material NewOne = new Material(childRenderer.material);
//mats.Add(NewOne);
mats.Add(childRenderer.material);
childRenderer.material = SilhouetteMaterial;
}
}
public void DetachSilhouette()
{
var childrenRenderers = objects.GetComponentsInChildren<Renderer>();
int t = 0;
foreach (var childRenderer in childrenRenderers)
{
childRenderer.material = mats[t];
t++;
}
mats.Clear();
}
However this problem is, executing function AttachSilouette() seems to give me a list of silhouette material instead of the original materials on these game objects that I want to cache. If I comment out the line childRenderer.material = SilhouetteMaterial; then it will actually give me a list of the materials that I want to cache.
Can someone tell me why this is happening? I figured that maybe the list only stores a list of reference so I tried creating a material instance before adding it to the list however it doesn’t work either.