- Home /
Assigning to Vector3 array.
Hi, I'm trying to do some mesh generation. Having some problems with C# arrays. I have a MeshFilter object mMesh and I try:
Mesh m = mMesh.mesh;
m.Clear();
float segs = 300;
m.vertices = new Vector3[(int)segs * 2-1];
for (int i = 0; i < segs; i++)
{
Vector3 ans = new Vector3(1,1,1);
Debug.Log(ans);
m.vertices.SetValue(new Vector3(ans.x, ans.y, ans.z), i);
Debug.Log(m.vertices[i]);
}
The first Debug prints out 1,1,1 but the second one prints out 0,0,0, I've tried using
mVertices[i] = ans; mVertices[i] = new Vector3(ans.x,ans.y,ans.z); and even mVertices[i].x = ans.x; ...
and none of them seem to work. Any help appreciated.
Answer by Eric5h5 · Feb 06, 2011 at 12:08 AM
Make sure to read the examples in the Mesh class docs. The code there is written like that for a reason; the mesh.vertices array is not a reference to the newVertices array, it's a copy, stored in VRAM. Individual values can't be written to it.
Ah, I've been doing much too much C++, I keeep forgetting about getters and setters. that totally did the trick. Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613103015im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
What is the order/connection of array elements in mesh.vertices? 2 Answers
Array.Push() for Vector3[] or how to add items to Vector3 array without knowing index 1 Answer
How to combine Vector3 arrays? 3 Answers
How to move an object and keep its distance relative to the player? 2 Answers
Store Vector3 from Array of GameObjects to Vector3Array 1 Answer