Shader property from script

Hey community,

Edit: nevermind me this is resolved and was just due to me not adhering to good practices and unknowingly having my script run on a different gameobject:roll_eyes:…

This probably just comes down to me not knowing how to use properties in my fragment shader but here’s what i am struggling with:
I am trying to set an float4 array in my shader via script. for debuggin I am currently doing so in the start() method via:

m = gameObject.GetComponent<MeshRenderer>().material;
shc1[0] = new Vector4(1f, 1f, 1f, 1f);
m.SetVectorArray("shc", shc1);

Debug.Log(shc1[0]);
Debug.Log(m.GetVectorArray("shc")[0]);

I have also tried:

var renderer = gameObject.GetComponent<Renderer>();
renderer.material.SetVectorArray("_shc", shc1);
renderer.material.SetVector("_justOne", shc1[0]);

In theory eveything prints fine, the array is initialised at the same size as the property specified in the shader, i am just overwriting the first entry here to make debugging simpler. The output of both debug.logs is the same. which makes me think i am simply using properties the wrong way in my shader.

However if i try to use the property in my fragment shader to just return the value of the first entry (which i manually set to (1,1,1,1) in line 1 of c# snippet) my gameobject is still rendered black.

Here is some shader code:

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"

float4 shc[9];
...

...
fixed4 frag(v2f i) : SV_Target
            { return  shc[0];}

What am I doing wrong?
Am i supposed to declare the float4 shc[9] somewhere else than in between CGPROGRAM / ENDCG? Do i need to specify somehow in the shader that this property is going to change?
I can’t find any examples
´

Thanks for your time

This should work, what specific Unity version are you using?

Also can we see the whole shader?

1 Like

Nevermind :sweat_smile:

For debug purposes i started logging material.shader.name and was promptly returned a different shader, after trying to figure out why and in the end reassigning my shader to the material via script, i figured out that I apparently had this script running on a different gameobject with a different shader attached so that i didn’t see anything happening on screen. :roll_eyes:

Lack of sleep is getting to me :eyes:

Thanks regardless

1 Like