- Home /

# 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)

-Pic2 (The results i am getting when i move the Cube and its Spheres)

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[i] = new Vector3(x * offset, 0);
// need the cube for parenting spheres, keep it
var cube = Instantiate(cubePrefab, cubePoints[i], 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<MeshFilter>().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[i] = spherePoints[i];
for (int i = 0, y = 0; y <= sphereAmount; y++)
{
for (int x = 0; x <= cubeAmount; x++, i++)
{
vertices[i] = new Vector3(x * offset, 0, y + 1);
uv[i] = 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[i], 0.1f);
}
Gizmos.color = Color.green;
for (int i = 0; i < cubePoints.Length; i++)
{
Gizmos.DrawCube(cubePoints[i], gizmoCubeSize);
}
}
}
```

**Answer** by Bunny83
·
Aug 11, 2016 at 12:16 PM

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[i] = transform.InverseTransformPoint(spherePoints[i].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<MeshFilter>().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[i] = transform.InverseTransformPoint(spheres[i].position);
mesh.vertices = vertices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
}
}
```

I am getting ERROR error CS0029: Cannot implicitly convert type `UnityEngine.Transform' to `

UnityEngine.Vector3' with spherePoints = new Vector3[(sphereAmount + 1)];(line 34) after i changed Vector3[] spherePoints to Transform[] spherePoints, would you know how to fix this?

Thanks a lot, i did not know you could do "cubes = new Transform[zSize];" i thought you could only do that with Vector3's.

### Your answer

### Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

### Follow this Question

### Related Questions

How do i create a Mesh in the middle of a Catmull spline? 0 Answers

How do i replace a Mesh Filter?? 1 Answer

Getting errors with extrusion script. 0 Answers

How do i code a Mesh collider ? 1 Answer

How does the Triangles work on Mesh? 1 Answer