# How do i have a Vector3[] position be in another Vector3[] position (That Updates)??

Hello i am having some trouble linking the vertices(Vector3) to the RedSpheres(Vector3) so i can manipulate the vertices,

to see what i am talking about see Pic1.

And in Pic2 are the results i am getting, does anyone know how to fix this?

-Pic1 (What i am trying to achieve)
[75897-capture14.png*_|75897]

-Pic2 (The results i am getting when i move the Cube and its Spheres)
[75900-capture13.png*_|75900]

And here is the code so you know what i have done.

``````using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class ProceduralRoad : MonoBehaviour
{

private Vector3[] cubePoints;
private Vector3[] spherePoints;

public GameObject cubePrefab;
public GameObject spherePrefab;

private Vector3[] vertices;
private Mesh mesh;

public int cubeAmount, sphereAmount, offset;
//private int xSize, zSize;
private Vector3 gizmoCubeSize = new Vector3(0.1f, 0.1f, 0.1f);
void Start()
{

//xSize = cubeAmount;
//zSize = sphereAmount;

cubePoints = new Vector3[(cubeAmount + 1)];
for (int i = 0, x = 0; x <= cubeAmount; i++, x++)
{
cubePoints _= new Vector3(x * offset, 0);_
``````

// need the cube for parenting spheres, keep it
var cube = Instantiate(cubePrefab, cubePoints*, Quaternion.identity) as GameObject;*
spherePoints = new Vector3[(sphereAmount + 1)];
cube.transform.SetParent(this.transform);

for (int j = 0, z = 0; z <= sphereAmount; j++, z++)
{
spherePoints[j] = new Vector3(0, 0, z + 1); // +1 because otherwise the first sphere is within the parent cube
// You might want to track the sphere instead of the vec3?
var sphere = Instantiate(spherePrefab, Vector3.zero, Quaternion.identity) as GameObject;
// parent
sphere.transform.SetParent(cube.transform);
// do positioning relative to parent
sphere.transform.localPosition = spherePoints[j]; ;
}
}
}
void Update()
{
Generate();
}

private void Generate()
{

GetComponent().mesh = mesh = new Mesh();
mesh.name = “Procedural Road”;

vertices = new Vector3[(cubeAmount + 1) * (sphereAmount + 1)];
Vector2[] uv = new Vector2[vertices.Length];

//I’m pretty sure this does nothing.
for (int i = 0; i < spherePoints.Length; i++)
vertices = spherePoints*;*

for (int i = 0, y = 0; y <= sphereAmount; y++)
{
for (int x = 0; x <= cubeAmount; x++, i++)
{
vertices = new Vector3(x * offset, 0, y + 1);
uv = new Vector2((float)x / cubeAmount, (float)y / sphereAmount);
}
}
int[] triangles = new int[cubeAmount * sphereAmount * 6];
for (int ti = 0, vi = 0, y = 0; y < sphereAmount; y++, vi++)
{
for (int x = 0; x < cubeAmount; x++, ti += 6, vi++)
{
triangles[ti] = vi;
triangles[ti + 3] = triangles[ti + 2] = vi + 1;
triangles[ti + 4] = triangles[ti + 1] = vi + cubeAmount + 1;
triangles[ti + 5] = vi + cubeAmount + 2;
}
}

mesh.Clear();
mesh.vertices = vertices;
mesh.uv = uv;
mesh.triangles = triangles;
}
private void OnDrawGizmos()
{
if (vertices == null)
return;
Gizmos.color = Color.black;
for (int i = 0; i < vertices.Length; i++)
{
Gizmos.DrawSphere(vertices*, 0.1f);*
}
Gizmos.color = Color.green;
for (int i = 0; i < cubePoints.Length; i++)
{
Gizmos.DrawCube(cubePoints*, gizmoCubeSize);*
}
}
}
*
*
@Fattie

Change:

``````private Vector3[] spherePoints;
``````

into

``````private Transform[] spherePoints;
``````

Instead of storing the initial position into the array you would store the actual instantiated object into that array. When assigning the positions to the vertices you would need to use “.position” of each point.

Furthermore the positions of each sphere is in worldspace. If the mesh parent object is not located at “0,0,0” you would need to use InverseTransformPoint of the mesh parent to convert the world position into a localspace position of the mesh. If you parent your spheres to the mesh you can actually use “.localPosition” directly.

In your line 68 - 75 you again overwrite the content of your vertices which doesn’t make much sense.

You probably only want:

``````for (int i = 0; i < spherePoints.Length; i++)
vertices _= transform.InverseTransformPoint(spherePoints*.position);*_
``````

You also want to create all those array once outside of your “Generate” method and reuse them each time you want to update the mesh. Since your uv array never changes you could create that in Start as well.
edit
You probably want something like this:
using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class ProceduralRoad : MonoBehaviour
{
private Transform[] cubes;
private Transform[] spheres;

public GameObject cubePrefab;
public GameObject spherePrefab;

private Vector3[] vertices;
private Mesh mesh;

public int xSize = 5;
public int zSize = 5;
public float offset = 1f;

void Start()
{
cubes = new Transform[xSize];
spheres = new Transform[xSize * zSize];

vertices = new Vector3[spheres.Length];
Vector2[] uv = new Vector2[vertices.Length];

for (int x = 0; x < xSize; x++)
{
var cube = (GameObject)Instantiate(cubePrefab, new Vector3(x * offset, 0), Quaternion.identity);
cubes[x] = cube.transform;
cube.transform.SetParent(this.transform);

for (int z = 0; z < zSize; z++)
{
var sphere = (GameObject)Instantiate(spherePrefab, Vector3.zero, Quaternion.identity);
spheres[z + x * zSize] = sphere.transform;
sphere.transform.SetParent(cube.transform);
sphere.transform.localPosition = new Vector3(0, 0, z + 1);
uv[z + x * zSize] = new Vector2((float)x / (xSize - 1), (float)z / (zSize - 1));
}
}
GetComponent().sharedMesh = mesh = new Mesh();
mesh.name = “Procedural Road”;
int[] triangles = new int[(xSize-1)*(zSize-1) * 6];
for (int ti = 0, z = 0; z < zSize-1; z++)
{
for (int x = 0; x < xSize-1; x++, ti += 6)
{
int vi = x * zSize + z;
triangles[ti ] = vi;
triangles[ti + 1] = vi + 1;
triangles[ti + 2] = vi + zSize;

triangles[ti + 3] = vi + zSize;
triangles[ti + 4] = vi + 1;
triangles[ti + 5] = vi + zSize + 1;
}
}
mesh.vertices = vertices;
mesh.uv = uv;
mesh.triangles = triangles;
UpdateVertices();
}

void Update()
{
UpdateVertices();
}

private void UpdateVertices()
{
for (int i = 0; i < spheres.Length; i++)
vertices = transform.InverseTransformPoint(spheres*.position);*
mesh.vertices = vertices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
}
}