I’m attempting to clone a material and apply it to an object at runtime. All properties seem to carry over, but for some reason the transparency stops working.
Script:
using UnityEngine;
public class MaterialApply : MonoBehaviour
{
public Material copyMaterial;
private void Start ()
{
Material newMaterial = new Material(copyMaterial);
GetComponent<MeshRenderer>().material = newMaterial;
}
}
On the left is an object with the material applied inside the editor. You can see the transparency working fine. On the right is a blank cube that the material will be copied to at runtime:
Here is the result after the material is copied at runtime:
You can see the material sort of copied over, but the object is opaque.
If I select the target cube, you can see all the properties are there, including alpha:
Any idea why this is happening? I suspect there is just some UpdateMaterialType()
or similar function I need to call somewhere.