- Home /
Vertex transformation - MissingFieldException error
I'm getting MissingFieldException: UnityEngine.Vector3.transform when trying to use a script. I don't know whats wrong with it and it's frustating me.
function Update () {
var mesh : Mesh = GetComponent(MeshFilter).mesh;
var vertices : Vector3[] = mesh.vertices;
var normals : Vector3[] = mesh.normals;
for (var i = 0; i < vertices.Length; i++)
vertices[i].transform.position = vertices[i].transform.position + Vector3(0,Random.Range(-1,1),0);
mesh.vertices = vertices;
}
Can somebody help me ?
Answer by robertbu · Aug 13, 2014 at 01:11 AM
Vertices in a mesh are Vector3 objects. Vector3s don't have a transform or a position. To move one you would do:
vertices[i] += Vector3(0,Random.Range(-1.0,1.0),0);
Note that your original code had integers in the Random.Range(), so the code would only have returned values -1 and 0 (since the integer version of Random.Range() is exclusive of the final value passed). I changed it to use floats.
Just nitpicking, but actually you are now using doubles. Type 1.0f to get floats - but it shouldn't make a big difference as you don't have any decimal value given anyway.
Nope, not doubles. The use of 'function' in his code above indicates that he is using Javascript. Decimal numbers are floats by default in Javascript. It is C# that has doubles by default. And you'd need a 'new' operator in front of 'Vector3' for C#.
Answer by WhoRainZone1 · Aug 13, 2014 at 01:14 AM
Only GameObjects do have transform, your vector array doesnt, as it is no GameObject.
vertices[i] += Vector3(0,Random.Range(-1,1),0);
This should work. But why? Because you are now using the vector's values, instead of trying to access something that isn't even there.
Cheers, WRZ
edit: oops, too slow :)
Your answer
Follow this Question
Related Questions
Converting Mesh to Set of Discrete Coordinates 1 Answer
Can I make animations snap to a frame? 1 Answer
hypercube animation 2 Answers
Rotating An Object On Its Axis Once 3 Answers