0
\$\begingroup\$

When I do a color exchange directly on the material in playmode, but then I go back to editmode, why do the settings still remain?

For example:
mymaterial.SetColor("_Color", blue);
or
mymaterial.color = blue;

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

This actually has nothing to do with SetColor and color, and everything to do with the difference between Scene Objects and Assets.

For data in your scene, like Game Objects in your scene hierarchy and their Components, as well as some lighting settings, Unity saves a copy of the open scenes just before entering Play Mode. When you exit Play Mode, it restores the saved copy, effectively erasing/resetting any changes that occurred in your scene while the game was running.

But asset files like Materials, Textures, Scriptable Objects, prefabs of Game Objects etc. do not live in your scene. They're their own independent asset files in your project folder. So they're not swept up and saved this way when entering Play Mode, or reverted afterward.

This can be useful, if you want to tune a material to look just right, or a set of gameplay parameters to feel just right in a particular moment of play, then exit play mode and keep your changes.

If you don't want to modify your source asset in this way, then you should create a copy of it. This will occur automatically if you use the Renderer.material or Renderer.materials property getters. But you can also copy it manually with:

var myMaterial = Instantiate(sourceMaterial);

Then you can modify myMaterial without those effects spilling over into persistent changes to your source asset on-disc.

\$\endgroup\$
1
  • \$\begingroup\$ thank you worked perfectly. \$\endgroup\$
    – user126378
    Jan 2, 2021 at 17:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .