Generating new materials problem when making a prefab.

This is going to be hard to explain since I don’t know the exact terminology.

I am making materials in the editor (dynamically) and applying them to generated mesh, but my problem comes when I want to turn my mesh into a prefab (with drag and drop into projects and with prefab utility),
I get the “No pixel shader pink” when I try and see the prefab.

This was my code:

obj.GetComponent<Renderer>().sharedMaterial = new Material(Shader.Find("SomeShaderIMade"));

I would like to keep that kind of format but I know you have to have some sort of material instanced, I have gotten it to work with using resources but it shares the material along all the objects and I need separate colors on them.

obj.GetComponent<Renderer>().sharedMaterial =  Resources.Load("test") as Material;

Is there a way I can do this where I can bring in new materials and have my generated mesh produce correct prefabs?

Resources.Load() will essentially generate a pointer to the prefab.

If you want to copy it, you can use Instantiate(loadedMaterial)

Also, using sharedMaterial could be an issue. You might need to use material instead.

I figured it out:

    int matcount = 0;
    private void ColorObject(GameObject obj)
    {
        matcount++;
        Material mat = obj.GetComponent<Renderer>().sharedMaterial = new Material(Shader.Find("MyShader"));
        AssetDatabase.CreateAsset(mat, string.Format("Assets/MyMaterial{0}.mat", matcount));
        obj.GetComponent<Renderer>().sharedMaterial.color = color;
    }

You need to create the material into the project so it has a dynamic link to a material with AssetDatabase. also if you go this way don’t create a new material every time if it’s the same color just share the material across all objects that share color.