Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Access material from code that was overridden in URP Renderer

Discussion in 'Scripting' started by emilerik, Oct 5, 2022.

  1. emilerik

    emilerik

    Joined:
    Aug 15, 2022
    Posts:
    2
    Hello!

    I have a URP Renderer which is used to add a hologram effect to some objects by overriding material for targets in a layer mask. However, now I need to access this overridden material from code to change its color, but I only seem to be able to access the default material for the object though. How do I access the overridden material from code?

    Thanks!
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,191
    Needs more info, perhaps a code snippet. Is this about Material Property Blocks? Or as simple as accessing .material rather than .sharedMaterial?
     
  3. emilerik

    emilerik

    Joined:
    Aug 15, 2022
    Posts:
    2
    Thanks for the reply.

    I'm using the Universal Render Pipeline (URP), not Material Property Blocks.

    I have an object called "Avatar" which has a material called "AvatarBody" as default. Sometimes, I set its layer to "Hologram" which adds a hologram effect using RenderObjects in URP.

    Now, if I execute the following snippet after adding the hologram layer to an avatar,
    Debug.Log(_currentAvatar.gameObject.GetComponentInChildren<Renderer>().materials)

    it will only return the "AvatarBody" material, not the "Hologram" material, even though I can see it is added to the Avatar.

    upload_2022-10-5_11-16-27.png
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,191
    Hmmm I'm not familiar with RenderObjects. But from the looks of it, this Hologram material does not seem to be part of the MeshRenderer material list. You probably need to access the RenderObjects instance in some way and access its override material property.

    Since the object is on the Hologram layer, you can use the layer to determine if it is currently using that RenderObject material - in case RenderObject is a global object, like ScriptableObject.