Fix All Broken Materials

Hey everyone, this is my first time posting here so I may get some of the traditional elements of a proper post incorrect.

I was wondering if someone can point me in a direction, or just give me a script that can find all of the “Hidden/InternalShaderError” having Materials and set the shader to something LIKE the standard shader. Ofcourse, I will be changing the end result of whether its a standard shader or something else, but for now I’m searching for a solution thats faster than just doing it all manually. This is being done in the UnityEditor scripts obviously.

Thanks.

Sorry, I meant to say “Hidden/InternalErrorShader”

Have you switched rendering pipelines? There’s a wizard for converting all your materials after switching.

The thing is that i don’t want to switch the rendering pipeline as it wont fix the issue im having. I just need a script that loops through every material that is in the assets folder, stores the materials that are missing shaders or are “broken” in a Materials array, and then applies the Standard shader to it or some other shader by name. Im really new to unity scripting so the api is a little confusing for me still.

I just meant there’s an automated way to do it already if they broke because you switched rendering pipelines.

Anyways, you’re going to have to get your hands dirty with some editor scripting for this. You’ll want to use the AssetDatabase to find all of the materials. Loop through and load them all, then evaluate the shader they’re using. Assign whichever shader you want back and then save your changes.

Figured it out. I created this method with the help of a friend:

private static void fixMaterials()
{
string[ ] guids = AssetDatabase.FindAssets("t: material");
foreach (string guid in guids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
Material temp = (Material)AssetDatabase.LoadAssetAtPath(path, typeof(Material));
if (temp != null && (temp.shader.name == "" || temp.shader.name == "Hidden/InternalErrorShader" || temp.shader.name == null))
temp.shader = Shader.Find("Standard");
}
AssetDatabase.SaveAssets();
}
3 Likes

Appreciate your sharing your solution!