How do I find the length of an array within a shader before setting it from a script?
I've been coding a grid-based warping shader to correct projected images. The shader exists as an image effect. Everything is working great, but there's just one problem. Here's the declaration of the arrays that I use for the data within the shader...
static const int GRID_SIZE = 15;
float _WarpUDisplacements[GRID_SIZE * GRID_SIZE];
float _WarpVDisplacements[GRID_SIZE * GRID_SIZE];
In order to reduce the likelihood of bugs within my code, I want the arrays I pass into my shader to have the same length as the arrays within the shader. Being as the grid size is defined within the shader, I figured I might be able to do the following from my C# script...
int warpArrayLength = this.warpMaterial.GetFloatArray("_WarpUDisplacements").Length;
However, I'm getting an exception, as the returned array is null for some reason. Is there any way that I can get at either the lengths of the arrays within the shader before I set them, or to access the GRID_SIZE constant declared within the shader?
Does anybody have an answer for this, as I'm having to resort to declaring my constant in both the shader and the script (which I don't really like, as it could introduce bugs if they're different)?
Your answer
![](https://koobas.hobune.stream/wayback/20220612165247im_/https://answers.unity.com/themes/thub/images/avi.jpg)