My script changes enum values, but the shader doesn't change in the engine

Hello, I have a problem with my shader graph enum keyword where I have exposed it to script and then I am using the script to change the enum. The problem is I can see the enum being changed real-time in the inspector whenever I change it by my dropdown UI in game but the shader doesn’t update and just stays as normal.

When I change it myself in the inspector, either on run-time or not, the shader works perfectly, but whenever I am trying to change the enum through code it refuses to work.

So I know my dropdown menu and the script attached to it works and that the enum gets the command to change but the shader it self doesn’t do anything. I also know by many different tests that the shader works on run time and in the editor. Where might the problem be?

Here is my code changing the enum itself:

namespace TPSBR
{
    public class SCR_ColourblindSettings : MonoBehaviour
    {
        [SerializeField] private Material shaderMaterial;
        private MeshRenderer[] allRenderers;
        private int dropdownValue;

        void Update()
        {
           
            allRenderers = FindObjectsOfType<MeshRenderer>();
           
            foreach (MeshRenderer m in allRenderers)
            {
                if (m.sharedMaterial.shader == Shader.Find("Shader Graphs/MixingColoursShaderTexture"))
                {
                    if (dropdownValue == 0)
                    {
                        m.sharedMaterial.SetFloat("_COLORBLINDNESS_MODE2", 0);
                    }
                    if (dropdownValue == 1)
                    {
                        m.sharedMaterial.SetFloat("_COLORBLINDNESS_MODE2", 1);         
                    }
                    if (dropdownValue == 2)
                    {
                        m.sharedMaterial.SetFloat("_COLORBLINDNESS_MODE2", 2);
                    }
                    if (dropdownValue == 3)
                    {
                        m.sharedMaterial.SetFloat("_COLORBLINDNESS_MODE2", 3);
                    }
                }
            }
           
        }

        public void ChangeColourblindnessMode(int val)
        {
            dropdownValue = val;
        }
    }
}

Here is the shader and how it works:

Try disabling Asynchronous shader compilation, that worked for me. I think there’s a bug with shader keywords and that feature.

You’re using sharedMaterial as opposed to material, so that’s good. That has added to the muddy water, figuratively, that this causes.

I should say that it worked more. Still having issues. The first two options in my Enum seem to work, but when I used the last two options it does not work. Shader keywords use what’s called shader variants.

I am wondering if the Enum Keyword Shader Graph Node is bugged. Going to continue to debug this. Any ideas would be appreciated. Going to try the “Strict shader variant matching” option in Project Settings and see if that gives me any insight.

Hey, sorry to necro this, but I’m struggling with controlling a shader graph enum via script and even getting it to work at all in the first place.

Could you guys please give me any tips about how to even get started with creating an inspector dropdown list that successfully drives the output values of the shader graph Enum?

I’ve gotten as far as creating the enum:

but I don’t know where to begin with implementing it and connecting it to the shader graph…
9237336--1290660--upload_2023-8-20_22-58-59.png

I was using keywords with no problems previously, but wanted to convert some stuff to use enums instead. The regular SetKeyword did not seem to work, I needed use the one that uses the LocalKeyword struct instead. As an added bonus, that method allows to use SetKeyword instead of the clumsy Enable/Disable pair, so code becomes a bit more clean:

  public static void SetCharacterType(Material material, CharacterType ct)
        {
            var localKeywordNormal = new LocalKeyword(material.shader,"_CHARACTERTYPE_NORMAL");
            var localKeywordBoss = new LocalKeyword(material.shader,"_CHARACTERTYPE_BOSS");
            var localKeywordGhost = new LocalKeyword(material.shader,"_CHARACTERTYPE_GHOST");
           
            material.SetKeyword(localKeywordNormal, ct == CharacterType.Normal);
            material.SetKeyword(localKeywordBoss, ct == CharacterType.Boss);
            material.SetKeyword(localKeywordGhost, ct == CharacterType.Ghost);
        }

Hope this helps.

It worked, thanks a lot!