Search Unity

Question Updating a material at edit time

Discussion in 'General Graphics' started by bakelord, May 11, 2023.

  1. bakelord

    bakelord

    Joined:
    Jan 19, 2014
    Posts:
    8
    Hi,

    I'd like to duplicate some prefabs, find their materials, duplicate those and then change the settings on them before writing them back into my new prefabs. The trouble I'm having is that I'm unable to write the new material back into the sharedMaterials list on the renderer component. If I use materials instead, I get told off for using it at edit time and end up trying to write to null.

    So, what's the correct way to update a renderer's materials at edit time?

    Thanks,
    Stu
     
  2. bakelord

    bakelord

    Joined:
    Jan 19, 2014
    Posts:
    8
    OK, as always I found the answer immediately after posting. For anyone else who needs this, you need to copy the sharedMaterials, update the one(s) you want and then write the array back because, of course, it's a property.