Changing emission from script doesn't update until I click on the material tab in the editor

Hello,
Long story short, I’m kind of new to Unity, I’m using it for an ongoing research project, and I need to generate GameObjects from C# to play video on them through RenderTexture and VideoPlayers.
I have this code for my material, which sets the RenderTexture both on _MainTex and Emission so it doesn’t look dark, but I actually need to manually click on material in the editor (like in the video below) to get my emission to work properly
1oiqu8

RenderTexture currRenderTexture = new RenderTexture((int) sphere.video.width, (int) sphere.video.height, (int) RenderTextureFormat.ARGB32);
            Material currMaterial = new Material(Shader.Find("Standard"));
            currMaterial.globalIlluminationFlags = MaterialGlobalIlluminationFlags.None;
            currMaterial.SetTexture("_MainTex", currRenderTexture);
            currMaterial.SetTexture("_EmissionMap", currRenderTexture);
            currMaterial.SetColor("_EmissionColor", Color.white);
            currMaterial.EnableKeyword("_Emission");

Do you have any idea on how I could solve this ? I’m absolutly new to Shaers and those stuffs, I just need a solution that can generate as many of those spheres as I want with different videos, that’s why I need those RenterTextures !

My guess would be either it’s some order of operations issue with enabling the keyword last, or that you need to enable more than you think. The fact it works when you click the material in the inspector means that the editor GUI is enabling / setting something for you.

Rather than creating a new material, have a material already set up for emission in the editor and instantiate a copy of that material to use instead. That way all the values will copy over properly. Then change the main texture, emission map and colour on that instantiated copy.

1 Like

Thanks for the fast and quality answer ! I made it using a Material and then kind of copying it (actually applying to my gameobjects and accessing them through it which is kind of the same if I get it right)
EDIT: Is there a way to close the thread or something ? I can’t find it

I’m also running into this issue trying to programmatically change a material’s Emission properties. I have Emission enabled by default with color Black, and at runtime I set the color like this:

material.SetColor("_EmissionColor", Color.cyan);

But it doesn’t work. If I inspect the material at runtime in the Inspector, I can see Emission is toggled on and the color is updated to Cyan. Everything looks good in the inspector.

If I interact with any material property in the material Inspector (toggle emission, or some random other property), suddenly the Emission color takes effect!

I’d like to avoid making a bunch of material variants that only alter emission and manage swapping them, but if that’s the only way to do this then I guess that’s the way to go :slight_smile:

Edit: I’ve also tried updating the EmissionColor via MaterialPropertyBlocks, but I have the same issue.

I found a solution! Setting the Global Illumination to “None” to default, instead of “Baked”, for each material I was changing the EmissionColor values seems to have fixed it.

6 Likes