- Home /
Question by
No_Username_Found · Nov 16, 2018 at 02:44 AM ·
materialrendererinstancing
.sharedMaterial = Material is creating an instance?
I have a 'color palette' tool which visually shows an array of materials by instantiating a cube with that material, and when new cubes are created they are given the same material as the active cube from the palette. Here is the code attached to the objects in the palette:
public void SetMaterial(Material newMat)
{
Debug.Log("Setting material to: "+newMat);
foreach(Transform child in transform)
{
child.GetComponent<Renderer>().sharedMaterial = newMat;
}
Debug.Log("The material is set to: " + transform.GetChild(0).GetComponent<Renderer>().sharedMaterial);
}
public Material GetMaterial()
{
foreach (Transform child in transform)
{
if (child.GetComponent<Renderer>().sharedMaterial != null)
{
Debug.Log("PaletteCube is returning material " + child.GetComponent<Renderer>().sharedMaterial+ " from " + child.name);
return child.GetComponent<Renderer>().sharedMaterial;
}
}
return null;
}
The weird thing is, that when I call these functions back to back:
CubeObject.SetMaterial(gold);
CubeObject.GetMaterial();
The print out says:
Setting material to: gold
The materials were set to: gold
PaletteCube is returning material gold (instance)
The instancing is a problem, and I'm only using the .sharedMaterial property, so where is the instance being generated? Why does the inspector show that the assigned material is an (instance), while the debug from that Set() function says it is not?
Comment