- Home /
Material alpha not updating at runtime
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.
There is a method, $$anonymous$$aterial.CopyPropertiesFrom$$anonymous$$aterial($$anonymous$$aterial mat) that you can try. http://docs.unity3d.com/ScriptReference/$$anonymous$$aterial.CopyPropertiesFrom$$anonymous$$aterial.html
However I can't replicate the error as duplicating a material should also automatically duplicate all the properties. When you use Debug.Log(copy$$anonymous$$aterial.property) and Debug.Log(new$$anonymous$$aterial.property) are the values different?
CopyPropertiesFrom$$anonymous$$aterial has the same result. And yeah, I get the same properties from logging.
Here's a super quick example project with the problem: http://dropcanvas.com/y5c6x
You can see it by opening the Test Scene
and hitting play.
downloaded project, but scene has no objects oh, perhaps you are using pro version and I'm not?
I downloaded my file to confirm, there's definitely stuff in the scene. (Looks like I typod the name though, it's "Text Scene" that you should open)
This is a Unity 5 project btw. You wont be able to open it in an old version.
does it work when you use Instantiate<$$anonymous$$aterial>(...) ins$$anonymous$$d of new $$anonymous$$aterial(...)?
Answer by fredlllll · Mar 26, 2015 at 05:14 AM
Use Instantiate<Material>(...) instead of new Material(...)
Your answer
Follow this Question
Related Questions
Applying partly transparent texture to a 3d model made in Blender. 1 Answer
Shader that makes object transparent as light hits it 0 Answers
Edge Detection (effect normals) with water (transparent) error. 0 Answers
Transparent material on iPhone Android 3 Answers
How do you make an X-ray vision following the mouse? 1 Answer