How to change material of a meshinstance through code in C#?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By shashank96

I have a shader material created and saved as .gdshader. I want to apply this shader as a material to my MeshInstance. To do this I have the following code:


public Resource blueprintShader = ResourceLoader.Load("res://BuildingBlueprint.shader");
var materialCount = this.model.GetSurfaceMaterialCount();
		for (var c = 0; c < materialCount; c++) {
			this.model.SetSurfaceMaterial(c, null);
			// this.model.SetSurfaceMaterial(c, blueprintShader);
		}

The issue is, I am not understanding what type should the preloaded shader be? The SetSurfaceMaterial() method takes in a ‘Material’ type only.

Also is there any other way of apply shaders which might be easier to do?

:bust_in_silhouette: Reply From: shashank96

I just had to create a new Material resource and set the shader to BuildingBlueprint.shader. Then preload it as

    public Material blueprintShader = ResourceLoader.Load<Material>("res://BuildingBlueprint.material");
this.model.SetSurfaceMaterial(c, blueprintShader);