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
- Out-of-bounds memory access with multiple CanvasRenderers under a Canvas when using Mesh API
- Inspector tries to access file after it was deleted when the file was locked in Inspector window
- Changing Transform values in Search window Inspector loses focus while dragging and stopping mouse without releasing dragging action
- Saving changes on the dirty VFX Graph during the Play mode throws "The referenced script (Unknown) on this Behaviour is missing!" warnings
- VFX Graph Debug Info overlaps the "Initialize" block debug info by default
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");