Changing color via code results in weird behavior while using a custom shader within the VFX graph

Hey there, I have the following problem that I cannot seem to solve. I have a VFX graph with an exposed color property. This color is then given to an exposed color property of a custom shader the VFX graph uses. Additionally, I have a prefab (A) with a Visual Effect component that uses this VFX graph. Prefab A is then used as a child of two other prefabs, B and C. Via code, I set the color for the Visual Effect for prefab B to green:

_visualEffect.SetVector4("My Color", _green);

Where _visualEffect refers to the Visual Effect component of prefab A that is the child of prefab B. This works fine. In the inspector, the Visual Effect component shows green as the chosen color, and in the scene view, the game view, as well as the build, the effect appears green. Now, via code, I set the color for the Visual Effect for prefab C to blue:

_visualEffect.SetVector4("My Color", _blue);

Where _visualEffect refers to the Visual Effect component of prefab A that is the child of prefab C. This is where it gets weird. In the inspector, the Visual Effect component shows blue as the chosen color. However, in the scene view, the game view, and the build, the color switches between green and blue. This switching is especially weird because sometimes it only happens in the scene view while the game view shows the correct blue color, and other times it only happens in the game view while the scene view shows the correct blue color. A few times, it was also switching in both views, but at different intervals. I have tried to set the color in the renderer with:

GetComponent<Renderer>().material.SetColor("My Color", _blue);

That fixed the problem above but gave me another problem. Now, some of the visual effects cannot be seen, although the game objects that contain them are active and the color is set correctly in the inspector. If I play around with the color property in the inspector, the Visual Effect is immediately visible again. Once I restart the game (without closing Unity), it is also immediately visible with the correct color. Once I restart Unity completely, it is invisible again until I play around with the inspector property. I am VERY clueless as to what is happening. Does anyone know what kind of wizardry is happening here? I really appreciate any kind of intel and fixes!

I am using URP and Output ParticleStrip Unlit Quad

Can you show us some gif or other resources of how it looks like?

First, if you set color of the renderer and it belongs to visual effect, don’t do it as you are not supposed to do this. You should do this only via visual effect component.
Second, is there only one prefab on scene? Perhaps you put two effects in the same place and there is some Z fighting.
Third, are you 100% sure you are setting property in prefab C? It seems the problem is on your side, for instance you have two scripts fighting to set the color.

1 Like

EDIT: Embedded images did not work, so I attached them at the bottom labeled with numbers. I had to put another post with the attachments, because I could not upload enough files.

Hey, first of thank you for your fast response. Screenshots would have been a smart thing to provide, sorry for missing those. I have written a small example that showcases the weird behavior I am experiencing. I am not sure if I am doing something wrong and if so what it is, so I’m going to dump a few screenshots of what I am doing, maybe you can see the mistake?

So, I have a scene with a spawner that spawns prefabs B and C
[Images 1_spawner and 2_spawner]


B and C are compositioned as follows
[Images 3_outer and 4_outer]

So they contain Prefab A and have a script that sets the color in prefab A.

A looks like this
[Images 5_inner and 6_inner]

So it sets the color of the visual effect via SetColor.

The relevant part of the visual effect and the shader it uses looks like this

[Images 7_vfx and 8_vfx]

This setup goes from working
[Image 9_working]

to not working
[Image 10_notWorking]

As the second screenshot shows sometimes the second visual effect has the color of the first one (it switches to it and then back again and then to it again and so on). I also just realize while making this example, that the colors are always shown correctly in the little main camera view. I have tried putting this in multiple projects (blank or already containing other things) and the error still persists.

9733945--1391992--1_spawner.png
9733945--1391995--2_spawner.png
9733945--1391998--4_outer.png
9733945--1392001--3_outer.png
9733945--1392007--5_inner.png

The rest of the images for above





To be honest I can’t see any errors. The only potential cause of this could be instancing, so try to disable instancing in the visual effect components and see if that still happens.
If that fixes the issue, then I guess it’s a bug in vfx graph, otherwise you can export your example as asset package and attach here, so we can try to reproduce this.

2 Likes

Oh, wow, that worked! Thank you so much for your time and help!

Good to hear it worked, however this should not happen as far as I know, so please report this bug with your example if it’s not a problem. Also you should check the lastest minor version of your unity editor as this problem might be already fixed there.

2 Likes

Hi pap3rhat,
As Qriva said, that looks like a bug on our side.
If you could attach a small repro project, it would be very helpful.
Also, could you tell us which version are you using?
Thanks for reporting this, and thank you Qriva for your help!

2 Likes

Hey, sorry for getting back to you so late, I did not realize I had new responses after my last message. I have uploaded a repo of a project showcasing the problem here: https://github.com/pap3rhat/Arch-Problem-Showcase, the prefab called “Inner prefab” in the “Arch Problem” folder contains the visual effect component. I am using Unity URP 2022.3.10f1 with the visual effect graph package 14.0.8.

While setting up this project, I noticed that after I turned “Allow Instancing” off one time and then turned it back on, the bug was gone, even after restaring everything (including my PC). However, in my other projects into which I moved the assets, that was not the case. So maybe it works off the bat for you and is just weird for me in every project except an empty one? The light and render pipeline settings are the same in all projects, the other projects just already contained assets. I honestly have no idea what is going on anymore ^^’

Edit: I did just realize what I have done differently. Instead of using “Universal” as an active traget in the used shader graphs and setting “Support VFX Graph” to true, my emtpy project (the attached repo) uses “VIsual Effect” as an active target. So it seems to be broken if you use “Universal”. I did have to change this in my attached repo, because my old way of doing it did no longer work. Interesintgly enough using “Universal” and setting “Support VFX Graph” to true does no longer work in any of my projects when I am trying to add a new Shader Graph that I can uses for my VFX Graphs.