Why do materials created at runtime show up incorrectly in the inspector?

Hi Everyone,

I have been searching everywhere for a solution for this but I just can’t seem to find it. Basically, I am creating a material at runtime that is a copy of an existing material. Then I assign it to a GameObject and it appears correctly from a graphics point of view, BUT, the details for the new material are not available in the inspector.

Is this just an inherent limitation with the editor / materials? Or is there some sort of flag that I need to set in order for a runtime material to become visible?

Here is a screenshot of what it shows in the editor:

And here is a very simple script that you can use to see what I am talking about:

public class SimpleMatCopy : MonoBehaviour
{
    public GameObject ObjA;
    public GameObject ObjB;
    
    // Start is called before the first frame update
    void Start()
    {
        Material mat = new Material(ObjA.GetComponent<Renderer>().material);
        mat.CopyPropertiesFromMaterial(ObjA.GetComponent<Renderer>().material);
        mat.name = "ObjB_mat";
        ObjB.GetComponent<Renderer>().material = mat;
    }
}

(Just assign GameObjects to ObjA and ObjB and make sure that they have different materials applied to them before running.)

I have also tried using:

Material mat = new Material(Shader.Find("Standard"));

but I got the same results.

I vaguely remember reading somewhere that you have to use Shader.EnableKeyword() in certain situations to get some of the properties to appear correctly but I tried all the standard shader’s keywords and it didn’t seem to have any impact on my issue. That being said, I might have done it wrong.

Thank you in advance for any help/ clarifications you can provide.

So out of the Box i would assume that happens because you got “No Shader selected” i assume you already notice that as well,

you will need to assign a “Shader” to your Material, you will also need to have the “Renderer” declared as variable,

take a look here at the wiki:
Unity Wiki