It’s hard to explain so I made a quick video: - YouTube
Note that the position works correctly when the grid is placed at the world-origin.
I recreated the problem with 2 simple primitive cubes attached to each other through code, but those don’t work either (you can see that the attached testcube is not rotated). What’s going on here? Why does the 2nd cube not inherit it’s paren’s rotation?

using UnityEngine;
using System.Collections;
public class PlacementTest : MonoBehaviour
{
void Awake()
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.name = "TestCube";
cube.transform.localScale = new Vector3(8, 8, 8);
cube.transform.position = new Vector3(10, 20, 10);
cube.transform.parent = transform.Find("test").transform;
//transform.Rotate(170, 0, 0);
}
}
Setting the world angles manually (just once) seems to solve this problem at least. Though I do not understand why I have to do this MANUALLY.
using UnityEngine;
using System.Collections;
public class PlacementTest : MonoBehaviour
{
void Awake()
{
CreateCube(new Vector3(10, 20, 10));
CreateCube(new Vector3(0, 0, 10));
}
private GameObject CreateCube(Vector3 location)
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.name = "TestCube";
cube.transform.localScale = new Vector3(8, 8, 8);
cube.transform.localPosition = location;
cube.transform.parent = transform.Find("test").transform;
//transform.Rotate(170, 0, 0);
//cube.transform.localEulerAngles = transform.localEulerAngles;
cube.transform.eulerAngles = transform.eulerAngles;
return cube;
}
}
I really don’t understand what possibly could be causing these position&rotational problems. I need to know that before I can fix the actual problem below I suppose.
For some reason the child-objects do not correctly move along with their parent’s position. But they do for a certain % of their parent’s transform…
And they don’t rotate at all… But rotating the grid AFTER the cells have been attached does correctly rotate them. I don’t understand why they don’t take their parent’s rotation when they are created and attached to the parent object.
// Creating a childobject and assigning it to it's parent
GameObject obj = Instantiate(GameSettings.Instance.PlacementCellReference); // More or less just a gameobject with a collisionbox
obj.transform.parent = transform.Find("Cells").transform; // Attach the cell to the grid's childnode called "Cells". Hierarchy: [Placementgrid] --> [Cells (empty gameobject)] --> [All the cells go here]
// Parent gizmo (purple grid from video)
void OnDrawGizmos(){
Gizmos.matrix = transform.localToWorldMatrix;
for (int y = 0; y < Rows; y++)
{
for (int x = 0; x < Columns; x++)
Gizmos.DrawWireCube(transform.position + new Vector3(x * PlacementCell.SIZE, y * PlacementCell.SIZE, 0), PlacementCell.SIZE_V3);
}
}
// How the child object positions itself (once):
transform.localPosition = new Vector3(index_x * SIZE, index_y * SIZE, 0);
Note that the childs also did not copy the parent’s rotation for some reason. The greenboxes that you saw in the video are the collisionboxes from the children (=PlacementCells).
EDIT:
Here is some more code just in case:
Grid (parent object):
void Awake()
{
Cells = new PlacementCell[Rows, Columns];
for (int y = 0; y < Rows; y++)
{
for (int x = 0; x < Columns; x++)
{
// Create a new game-object and attach it to the "Cells" node
GameObject newCell = Instantiate(GameSettings.Instance.PlacementCellReference);
newCell.transform.parent = transform.Find("Cells").transform;
// Perform the PlacementCell stuff here
//PlacementCell c = obj.AddComponent<PlacementCell>(); // old code
newCell.GetComponent<PlacementCell>().Initialize(transform.rotation, x, y);
Cells[x, y] = newCell.GetComponent<PlacementCell>();
}
}
}
Placement Cell (child):
public void Initialize(Quaternion gridRotation, int index_x, int index_y)
{
this.Index.X = index_x;
this.Index.Y = index_y;
// Set the position of the cell. Take into account that the grid may be rotated.
transform.localPosition = new Vector3(index_x * SIZE, index_y * SIZE, 0);
// Collider
Collider = gameObject.AddComponent<BoxCollider>();
Collider.name = string.Format("{0}{1};{2}", COLLIDER_PREFIX, index_x, index_y);
Collider.isTrigger = true;
Collider.size = new Vector3(SIZE, SIZE, TRIGGER_DEPTH);
}