how to get shader name in edit mode

I’m working on an editor script that will allow me to find objects that have a particular shader attached to them. Problem is, when I check the name of the shader a material is using with :

if(g.renderer.material.shader.name == shaderName)

I get the 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.

Is there another way to do this?

Try with this. it is printing “Diffuse”.

using UnityEngine;
using System.Collections;

public class Shader_find : MonoBehaviour {
void Start () {
if(renderer.material.shader.name==“Diffuse”)Debug.Log (“Diffuse”);
}
}

In case some case someone still wonder what’s the names (for scripting)in the new standard physic-based shader that comes with Unity 5.x, there’s a list with a visual reference :


The Green texts are float values (either Float, Float3 or Float4 as specified)
The Blue texts are the Texture2D so the actual maps you import in Unity.