Issue changing shader float

Hello! In a game I am working on, items flash when they are ready to upgrade, however I’ve ran into an issue with the flashing. I am using a shader to achieve the flashing effect that has a float on it that changes the intensity of the flash, well it works great until the player reloads the game, then something that was flashing before no longer flashes at all forever (The game saves).

Here is the code I use to make the shader flash:

if(flashing)
        {
            if (upgradeShineAmount >= 14)
            {
                addShineAmount = false;
            }
            else if (upgradeShineAmount <= 1)
            {
                addShineAmount = true;
            }
            upgradeShineAmount += addShineAmount ? Time.deltaTime * 5 : (Time.deltaTime * 5) * -1;
            GetComponent<Renderer>().material.SetFloat(Shader.PropertyToID("_RimPower"), upgradeShineAmount);
            DynamicGI.UpdateMaterials(GetComponent<Renderer>());
        }
else if(isActive)
        {
            upgradeShineAmount = 15;
            GetComponent<Renderer>().material.SetFloat(Shader.PropertyToID("_RimPower"), upgradeShineAmount);
            DynamicGI.UpdateMaterials(GetComponent<Renderer>());
        }

If I change the GetCompenent().material to a reference to the actual objects material it all works, except it will change it on other gameobjects using the same material, which I cannot do. I’ve googled a ton and cannot find a solution. Thanks for any help!

Alright, so I figured it out! When The player would upgrade I would switch the material to a new one for the new look of the object, and I guess switching materials causes either GetComponent().material.SetFloat() to break or Shader.PropertyToID() to break. Unsure. However here is my fix (Where I would change the material, I commented out the part that would break this):

public virtual void CreateMaterial()
    {
        //GetComponent<Renderer>().material = material[(int)levelType - 1];
        GetComponent<Renderer>().material.mainTexture = material[(int)levelType - 1].mainTexture;
    }

:smile:

If someone knows why this happens, or an explanation for why this happens, I would appreciate to know so I could learn from this :slight_smile: … Or is this a bug with Unity?

Thanks!