- Home /
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.
I have the same inspector bug. I have a custom shader made from the shader graph and when I assign a material with it at runtime the problem appears. I have to read the properties by using the inspector debug mode but it's insane. Anyone have a solution?
Sorry I didn't reply to you sooner but, unfortunately, I never found a solution or even any additional information. As far as I can tell, this is either a bug with the inspector or a seriously inconvenient expected behavior. It is extremely annoying for anyone working with materials at runtime. For example, procedural materials or procedurally generated copies/variants are basically impossible to inspect which makes it super challenging to fine tune them or troubleshoot issues. For that matter, any material that I create from script can't be inspected (even if it just creates a copy of an existing one).
If anyone ever finds a solution to this (or even just more information) I would be THRILLED!
Alternatively, I am considering writing a custom inspector to solve this because it is such a pain. I just don't want to bother writing one if there is an existing solution that I don't know about.
Double click on material field for that Renderer
component. This selects the material and thus shows it's inspector.
Answer by Terraya · Mar 30, 2021 at 02:32 AM
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
Your answer
Follow this Question
Related Questions
Dynamic batching breaks with the material Mobile/Alpha Blended. 0 Answers
Is this the correct way? 1 Answer
MaterialPropertyBlock causes Z-fighting sprites 0 Answers
How to apply a color to the second texture in this shader? 1 Answer
How to make a cube with metallic look and soft edges [pic included] 1 Answer