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
- Search field in UI Builder's Library has no character limit
- Changing the value in UI Builder by dragging increases it by a few thousand points when reaching the borders of the screen
- ARM64 architecture DLL incorrectly copied instead of x64 when building for Windows x64
- Crash on TypeTreeIterator::GetNode when opening a specific folder
- Crash on WriteSerializedAssetsV2 when (re)entering the Play Mode in a specific project when IEnumerator Start() is used
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");