Why do run-time changes to a material via a script persist in the Editor?
I am editing an instance of a Prefab at run-time via a script. In this script, I set the UV information of a Shader via a RawImage's material. After leaving 'Play' mode, the UV information of the original material in the Editor have been altered.
void SetShaderLighting(RawImage rawImage, int index, float x = 0f, float y = 0f)
{
string target = "_Lighting" + index;
Vector2 temp = rawImage.material.GetTextureOffset(target);
temp.x = x;
temp.y = y;
rawImage.material.SetTextureOffset(target, temp);
}
I believe this Behavior is due to Unity passing a direct reference to the original material in the Editor, thus any changes made to what is commonly referred to as the "sharedMaterial". However, this seems really inconsistent with all the other conventions in Unity were run-time data changes are lost. Does anyone know the reasoning for this behavior?
Answer by eses · Aug 22, 2018 at 05:39 PM
Hi @Omita
There is already an exhaustive answer to this question answered by Bunny83 - see link:
https://answers.unity.com/questions/487908/modifying-material-modifies-the-actual-mat-file.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612173244im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Accessing shader colors 1 Answer
Scaling a plane in ExecuteAlways mode removes color 0 Answers
Shadows passing through objects, why? 4 Answers
Hair material problem 0 Answers
Mix Shaders Question -- Multiple Textures, Recolorable? 1 Answer