- Home /
Changing the materials in Editor Mode Without Getting the leak Error
is there is any way to change the materials by code at editor time without countering this error
Instantiating material due to calling renderer.material during edit mode. This will leak materials into the scene. You most likely want to use renderer.sharedMaterial instead
in most cases i generate temp material this way
Material m = new Material(Shader.Find(SHADER_NAME));
or
renderer.material = new Material(Shader.Find(SHADER_NAME));
Answer by Ali-Nagori · Dec 31, 2013 at 09:01 AM
going to answer my own question , so it might help if any one else has the same issue
just use the sharedMaterial instead
and for case of array of materials for single object define a new array of materials make changes to it then assign it as objects shared materals
Material[] faceMaterials = new
> Material[4];
> faceMaterials[0] = matSetup( Color.white, faceTexture,
> facialTexture);
>
> faceMaterials[0].name = "Face";
> faceMaterials[1] = matSetup(Color.white ,eyeTexture);
>
> faceMaterials[1].name = "Eyes";
>
> faceMaterials[2] = matSetup(Color.white ,null);
> faceMaterials[2].name = "Teeth";
> faceMaterials[3] = matSetup(new Color(0.933f , 0.731f ,
> 0.731f , 1) ,null);
> faceMaterials[3].name = "Toungh";
> Facetransform.renderer.sharedMaterials = faceMaterials;
-----------------------
Material matSetup(Color col , Texture tex)
{
Material mat = new Material(Shader.Find("Diffuse"));
mat.SetColor("_Color",col);
mat.SetTexture("_MainTex",Resources.Load(tex) as Texture);
return mat;
}
This worked brilliantly!
I was struggling to find a solution to this that actually fit my problem; but swapping out the array worked a treat.
Answer by janivarattensi · Feb 15, 2016 at 04:11 PM
Thank you for posting your solution! I was having a similar problem. Assigning a new array to sharedMaterials instead of altering the existing one solved my problem :)
Answer by IgorAherne · May 17, 2018 at 03:39 PM
I was looking for something more like: http://answers.unity.com/answers/322397/view.html
Your answer
Follow this Question
Related Questions
Renderer.materials leaking materials 1 Answer
Index array is out of range (following H&S tut) 2 Answers
Setting materials at runtime 1 Answer
Missing Materials on build 1 Answer
Pressing play Causes Re-instancing of material (Editor) 1 Answer