Search Issue Tracker
By Design
Votes
2
Found in
2017.4.0f1
2018.4.0f1
2019.1.3f1
2019.2.0a1
2019.3.0a1
2020.1.0a1
Issue ID
1187202
Regression
No
<Renderer>().material changes made to a GameObject also apply to the instantiated GameObjects material
Steps to reproduce:
1. Open the project attached by the user
2. Open "SampleScene"
3. Enter Play mode
4. Press "1" -> GameObject is created
5. Press "a" -> color of the created GameObject is changed
6. Press "2" -> a GameObject is instantiated using the existing one
7. Repeat step 5, notice both of the GameObjects changing color in tandem
Expected result: Only the color of first created GameObject changes
Actual result: Color of both GameObjects changes
Reproducible with: 2017.4.32f1, 2018.4.10f1, 2019.2.7f2, 2019.3.0b5, 2020.1.0a6
Notes:
- Don't use keypad to input numbers
- "1" creates a GameObject, "a" changes the color of the original GameObject, "2" instantiates a GameObject from the original one
- If instantiation comes before applying changes to the material, the instantiated GameObject doesn't change when changing the original one's material
-
nickhudson2244
Oct 26, 2023 19:52
I know this is marked as resolved but I would love if Unity would address this issue. The whole .material/.sharedmaterial is extremely broken imo. Especially when using Unity to build non-game applications such as CAS software/runtime editors/etc.
In many cases, you would want to modify the instanced material (.material) and sometimes modify the shared material (.sharedMaterial). However, simply accessing the instanced material (.material), then reference to the sharedMaterial is lost. There is no way this is the desired behavior.
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- Articulation Body with 'Revolute' Joint Type has erratic behavior when Upper Limit is set to above 360
- WebGL Player fails to render Scene when Terrain with Detail Mesh is added and WebGPU Graphics API is used
- Inconsistent errors are logged when different types are passed into the Query "Q<>" method in UIToolkit and the ancestor VisualElement is null
- Crash on GetMaterialPropertyByIndex when opening a specific Scene
- Discrepancies in the styling are present when using a TSS file instead of a USS file in custom EditorWindow
Resolution Note:
When instantiating a GameObject using an existing GameObject, the material is shared.
Hence why the color changes in both gameobjects - they are using the same material instance.
If this is not desired, the material can be changed so it is no longer shared between both gameobjects.
To do this, Renderer.material property should be get, set or modified.
Example for setting -
myGameObject.GetComponent<Renderer>().material = new Material(Shader.Find("Standard"))
Example for getting -
Material myMaterial = myGameObject.GetComponent<Renderer>().material;
Example for modifying -
myGameObject.GetComponent<Renderer>().material.color = new Color(150.0f, 150.0f, 150.0f);
Why does getting or modifying instantiate the shared material?
Documentation for this is here: https://docs.unity3d.com/2019.1/Documentation/ScriptReference/Renderer-material.html
NOTE: If using getting or modifying to instantiate a material, Renderer.material will only automatically clone a shared material once per Renderer.