Search Issue Tracker
Won't Fix
Votes
0
Found in
2018.4
2019.4
2019.4.24f1
2020.3
2021.1
2021.2
Issue ID
1334668
Regression
No
Material Constructor doesn't copy ShaderKeyword from material in Play Mode
Reproduction steps:
1. Open the Project "case_1334668-MatConstructorError"
2. Open the Scene "Test Scene"
3. Play Scene
4. In Assets, click on "WhiteMaterial"
5. Enable 'Red' ShaderKeyword from Inspector window
6. Observe Game view
7. Assign "WhiteMaterial - Copy" to Sphere Mesh Renderer Component
8. Observe Game view
Expected result: Sphere is red like the color the original material was
Actual result: Material ShaderKeyword isn't copied and Sphere is white
Reproducible with: 2018.4.33f1, 2019.4.24f1, 2020.3.4f1, 2021.1.4f1, 2021.2.0a15
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
- [Quest] Pitch Shifter causes audio degradation on Quest 2 devices when sped up
- "GUI Error: Invalid GUILayout" and "NullReferenceException" are thrown when adding Scenes to "Scenes in Baking Set" in Adaptive Probe Volumes
- Inspector window not updating when switching GameObjects in Play Mode while having a VR headset connected
- "UnityException: GetName can only be called from the main thread." erors are thrown when the Shortcuts window is opened and entering Play Mode with a shortcut
- [Android] Light2D visuals stacking over frames when using RenderGraph
Resolution Note (2021.2.X):
An explanation of what's happening:
- When calling EnableKeyword, the keyword value is set internally on the material
- When the material is copy constructed, it property sets the keyword state
- Right after copying state, the material property drawer comes along and applies all of the properties (note, this is an editor only thing)
- the property is 0 (because setting the keyword doesn't set the property that the inspector uses) and so the keyword gets disabled.
This is something we can't really fix at runtime as properties are editor only and there is no mapping at runtime between the property name and the keyword name.
To work around this, you can set the property value as well as the keyword:
sharedMat.SetFloat("_isRed", 1);
sharedMat.EnableKeyword("RED");