- Home /
How do I store Vertices in an array?
Hi everyone! I'm looking to store vertices in an array that way I can move objects toward a specified point. I want to get the vertices from the edges of a simple plane for starters and then tell my cube to move toward that location. If anyone can help me I would be ever grateful! Here's my code so far:
using UnityEngine; using System.Collections;
public class CutMesh : MonoBehaviour { public Vector2[] uvCoordinates; public int[] newTriangles; public Transform cube;
private Transform vertex;
Vector3[] rightVertices;
Vector3[] leftVertices;
Vector3[] topVertices;
Vector3[] bottomVertices;
// Use this for initialization
void Start ()
{
Mesh piece = GetComponent<MeshFilter>().sharedMesh;
Vector3[] rightVertices = piece.vertices;
Vector3[] leftVertices = piece.vertices;
Vector3[] topVertices = piece.vertices;
Vector3[] bottomVertices = piece.vertices;
int i;
for (i = 0; i <= 11; i++)
{
rightVertices[i] = new Vector3(11, i, 0);
leftVertices[i] = new Vector3(0, i, 0);
topVertices[i] = new Vector3(i, 11, 0);
bottomVertices[i] = new Vector3(11, i, 0);
}
//vertex = piece.vertices;
}
// Update is called once per frame
void Update ()
{
cube.position = rightVertices[0];
}
}
I don't understand your code. You defined the vertices arrays as piece.vertices, and then overwrite them in that loop. What's the point?
Answer by whydoidoit · Mar 06, 2014 at 04:16 AM
You are redeclaring your arrays as local variables and hence Start does nothing useful.
private Transform vertex;
Vector3[] rightVertices;
Vector3[] leftVertices;
Vector3[] topVertices;
Vector3[] bottomVertices;
// Use this for initialization
void Start ()
{
Mesh piece = GetComponent<MeshFilter>().sharedMesh;
rightVertices = piece.vertices;
leftVertices = piece.vertices;
topVertices = piece.vertices;
bottomVertices = piece.vertices;
int i;
for (i = 0; i <= 11; i++)
{
rightVertices[i] = new Vector3(11, i, 0);
leftVertices[i] = new Vector3(0, i, 0);
topVertices[i] = new Vector3(i, 11, 0);
bottomVertices[i] = new Vector3(11, i, 0);
}
//vertex = piece.vertices;
}
// Update is called once per frame
void Update ()
{
cube.position = rightVertices[0];
}
}
Your answer
Follow this Question
Related Questions
Moving an Object to the vector of other objects on button press using a vector3 array? 0 Answers
NullReferenceException on assigning vector3 to mesh.vertices - no effect on assignment? 1 Answer
How to have a GameObject follow a path with c#? 3 Answers
[Long Post] Constant linear movement in a 2d array grid with auto-turning on detecting a wall 1 Answer