Array index out of range problem referencing GameObject Mesh.Vertices

What’s wrong with this line that it might receive an array index out of range error?
OldGen is a “new Vector3()” initialized variable. nameDynam2 should be right, though it loops multiple times changing an integer at the end. The object is definitely rendering its generated vertices as I’m walking across it when the error goes, so it should have vertices right? Is there an alternative way I should approach this?

OldGen = GameObject.Find(nameDynam2).GetComponent<MeshFilter>().sharedMesh.vertices[0];

Try:

Debug.Log(vGameObject.Find(nameDynam2).GetComponent<MeshFilter>().sharedMesh.vertices.length);

I bet the output is “0”. When you’re doing Find and GetComponent you shouldn’t chain all of that together either… it’s error prone. If vGameObject.Find(nameDynam2) doesn’t find a gameobject, then you’re going to get a NullReferenceException when it tries to call GetComponent() and likewise, if the MeshFilter doesn’t exist, you’re going to get a NullReferenceException when trying to access .sharedMesh.

Interesting, it appears it’s working for the first half of the loop and then getting 0 the second half. I’ll have to think on it, thanks.

Edit: Funny story… Some of the meshes in loop I’m running are appropriately empty because of the noise algorithm… thus, no vertices. Problem Solved… next problem: finding another way to reference the “origin” of the mesh without referencing the unity “origin”.

Does anyone know how to move a mesh? Or move a GameObject without moving the mesh attached to it?