Search code examples
c#unity-game-engineshader

Unity3d - set material to none


I have a UI image attached to a canvas. I have an OnClick function that changes the material of the Canvas Renderer for the image when it's clicked.

gameObject.GetComponent<CanvasRenderer>().SetMaterial(J_UI.UI.uiChosenSkillHolo,0);

I want a 2nd click to remove the material, so that in the inspector you see None (Material). I've tried

gameObject.GetComponent<CanvasRenderer>().SetMaterial(null,0);

but the image isn't visible at all, as if there were no shader.

I've also tried logging the current None (Material) to a material variable in the Awake function and using that but again the image disappears.

Printing the material name in both cases returns Null.

So - is there any way to get a handle on this mystery material called None (Material) which is the default in the inspector wherever a material is appropriate & you haven't assigned one yet?

Thanks.


Solution

  • Update: Realised I was going about this the wrong way so I'll post an answer in case anyone else is struggling with this...

    You need to change the material in the Image script directly, not use the canvas renderer. So to apply it's

    gameObject.GetComponent<Image>().material = yourMaterial;
    

    and to remove it's

    gameObject.GetComponent<Image>().material = null;