Updating a MeshRenderer's material works for placed objects, but not instantiated?

I have two identical instances of a prefab in a scene. One is placed there at edit time. One is instantiated at run time.

Then, I get the MeshRenderer component of each and try to update the material to use a new material (meshRenderer.material = newMat)

The prefab that was placed in scene updates its material as expected. The instantiated prefab does not. I am at a total loss as to why.

I tried debugging and found that the material property of the MeshRenderer does seem to get updated in both cases (at least according to the Visual Studio debugger Locals panel). However, if you look at the instantiated object in the Unity Inspector, it still shows the original material.

One more note, I can manually drag a material at runtime in the Unity Editor onto either MeshRenderer component and that seems to work fine as well.

Any idea what’s going on here?

Have you considered that it might be the inspector not updating?
(Check by having a obvious difference in material)

It’s definitely not an inspector problem. The material change is a very obvious visual change and the object looks no different.

Can you perhaps share the code you are using to do this?
As from what your saying it sounds like a critical unity bug. Or it could be a simple misstake in the code…

and never mind… very stupid mistake in code on my part.

@casimps1 Might I ask your mistake? I seem to be doing the same thing.

Nice necropost :). It probably was because of the renderer.material = someMat; I believe this assigns a copy of the material to the renderer, instead of a reference. Try using renderer.sharedMaterial instead, that takes a reference.