- Home /
Runtime created Material only works when clicking in inspector
Hi Guys, looking for advice on my code. I have an instantiated player prefab with a single material on the mesh. Everytime the game starts, i want him to be temporarily semi transparent. I decided to try to create a runtime-only material which has some transparency to it.
The code looks like this, but the problems are explained in the screenshot. I am getting all kinds of issues at runtime that seem to rectify only when i "click around" in the inspector of the runtime only material.
Material invisibleMaterial;
SkinnedMeshRenderer smr;![alt text][1]
smr = playerGO.GetComponentInChildren<SkinnedMeshRenderer>();
invisibleMaterial = new Material(smr.material);
Color invisibleColor = new Color(255f, 0f, 0f, 0.3f);
invisibleMaterial.SetFloat("_Surface", (float)SurfaceType.Transparent);
invisibleMaterial.SetFloat("_Blend", (float)BlendMode.Alpha);
invisibleMaterial.EnableKeyword("_ALPHABLEND_ON");
invisibleMaterial.SetColor("_BaseColor", invisibleColor);
invisibleMaterial.name = "invismat";
smr.material = invisibleMaterial;
[1]: /storage/temp/166889-untitled.png
untitled.png
(191.9 kB)
Comment