Question by
DeBugs · Jun 07, 2017 at 07:18 PM ·
editor-scriptingmaterialssubmesh
How to set a material to subobject in editor script?
Hi!
I am trying in an editor script to set a new material to an object which have sub-objects. what I am doing now:
gameobject.GetComponent<Renderer>().sharedMaterials[id] = new Material(Resources.Load<Shader>("Editor/shader"));
but when it doesn't do anything.
if I do that just after:
if (! gameobject.GetComponent<Renderer>().sharedMaterials[id])
{
Debug.Log("material is null");
}
I've got the message "material is null" (and in the inspector the material is null for the selected subobject) I checked that the Resources.Load doesn't return null and of course the "new Material" is not null either.
Whithout subobjects i can just set the gameobject.GetComponent<Renderer>().sharedMaterial
(without array) and everything works just fine.
Do you have any idea why?
Thank you.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612123723im_/https://answers.unity.com/themes/thub/images/avi.jpg)