Modifying materials from editor scripts

Hello. This is more of an attempt to snipe a Unity developer for some immediate feedback, as I’m pretty sure this issue has no external workaround.

I’m instantiating some objects in an editor script, and I need to be able to change properties of materials. Whenever I do this, I get an error that reads:

Instantiating material due to calling renderer.material during edit mode. This will leak materials into the scene. You most likely want to use renderer.sharedMaterial instead.

I don’t want to use renderer.sharedMaterial, because I’m trying to work with a few different objects, so modifying the sharedMaterial a second time will overwrite the changes I made to the first material.

I’m not instantiating a whole lot of objects (no more than have been placed by hand at one point), so I’m sure the “leaked materials” won’t end up being a huge problem, but nobody wants to see a mess of error messages when he clicks a button.

Are materials not instantiated outside of game mode? Is there anything we can do about this?

Bump

If you create several objects with the same material, then the same material properties apply to all the objects (ie, each object references a single copy of the properties rather than keeping its own separately modifiable copy). If you want to start with an given material and make several variants of it then you will need to create new material assets. You can do this with AssetDatabase.CreateAsset. The doc page for this function coincidentally has a code example for this exact task.

Excellent, thanks andeeee!

I thought I had exhausted every possible option, but I guess I should read the docs more carefully!

Cheers, mate.