Question by
ExtremeChaos · Jun 23, 2017 at 04:19 AM ·
script.cubegeneration
How do I change the coordinates of the GameObject to the first cube in the generated grid?
I have generated a grid from cubes and I need a way to re position the GameObject with the script GRIDGEN attached to the first cube (top left of the generated grid). I need to do this without moving the grid since the script generates the grid from the centre where the GameObject is.
Thanks.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GridGen : MonoBehaviour {
public GameObject cube1;
public int gridWidth = 10;
public int gridHeight = 10;
public float spawnSpeed = 0;
void Start()
{
StartCoroutine(CreateGrid());
}
private IEnumerator CreateGrid()
{
for (int x = 0; x < gridWidth; x++)
{
yield return new WaitForSeconds(spawnSpeed);
for (int z = 0; z < gridHeight; z++)
{
yield return new WaitForSeconds(spawnSpeed);
GameObject cube = Instantiate(cube1, Vector3.zero, cube1.transform.rotation) as GameObject;
cube.transform.parent = transform;
cube.transform.localPosition = new Vector3(x, 0, z);
}
}
}
}
capture.png
(253.5 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612124712im_/https://answers.unity.com/themes/thub/images/avi.jpg)