Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Setting the material of a MeshRenderer without instantiation?

Discussion in 'General Graphics' started by CorbeauDeh, Oct 6, 2019.

  1. CorbeauDeh

    CorbeauDeh

    Joined:
    Feb 2, 2015
    Posts:
    5
    I have a dynamically instantiated material which I want to apply to a bunch of dynamically instantiated GameObjects with different meshes. I want them to share the material. However, no matter what method of assigning a material to the MeshRenderer I try, it always ends up re-instantiating the material for each MeshRenderer. (I've tried assigning material, sharedMaterial, and their list variants.)

    It seems like setting the material without instantiation should be possible, since that's what happens if you assign it in the Editor.

    One half-workaround to this problem is using Graphics.DrawMesh instead, but since the objects share a material, DrawMesh refuses to order them.

    Edit: Apparently you can simply assign a material to the MeshRender. I think I was doing something wrong with per-instance material customization that led to it getting reinstantiated for each instance.
     
    Last edited: Oct 11, 2019
    RyanFavale likes this.