I have a project where I’m using vertex colours as data to procedurally stretch my meshes, ie red goes left, green: forward, blue: up, I don’t use them to render at all. At run-time I load the mesh and modify the vertices using the colour information. This works perfectly in debug, but in a built version the color data in the instanciated meshes is empty.
If I manually place a the meshes in the scene with a material that uses vertex colours this seems to force unity to know it has colours and it works fine. However this is an ugly hack, is there something I’m missing?
Here’s an example, just a single script and a basic model with vertex colours, the whole project was a bit big to upload, I’ve just put this together quickly to demonstrate the scenario.
You’ll notice it works fine in debug, but when you build it, it throws an out of bounds exception because the mesh.colors is empty.
Note: you need to turn on Read/Write Enabled on the mesh
Here’s the code again to make it more visible:
private void StretchMesh()
var instance = Instantiate(MeshObject);
var meshes = instance.GetComponentsInChildren<MeshFilter>().Select(v => v.mesh);
var moveDist = 10f;
foreach (var mesh in meshes)
var verts = mesh.vertices;
var colors = mesh.colors;
for (var i = 0; i < verts.Length; i++)
// On build this throw an index out of range exception because the colours are empty
verts_.x += moveDist * colors*.r;*_
var newMesh = new Mesh
vertices = verts,
triangles = mesh.triangles,
colors = mesh.colors,
normals = mesh.normals,
tangents = mesh.tangents,
uv = mesh.uv,
var go = new GameObject(“Test”);
var filter = go.AddComponent();
var renderer = go.AddComponent();
filter.mesh = newMesh;
renderer.material = TestMaterial;
No @axelucho, I ended up using fixed width meshes for the meantime while I concentrated on other parts of the project. Unless the latest version of Unity fixes it, it doesn’t look like a viable solution.