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

Shader System

-

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

  1. 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");

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.