Question by
unity_PKtq_cAk5SVQzg · Feb 22, 2021 at 04:09 PM ·
instantiateaxispivot
Instantiate at end of GameObject
Hello
I am trying to CreatePrimitive cubes with values from an array of Scale Vector3's. This basic code works as needed but I want the 2nd cube to line up at the end of the 1st, then the 3rd at the end of the 2nd and so on...
It looks like because the axis is in the centre of the GameObjects they are instantiated at that point. I've tried many times with * / + calculations but can't get right. Please help.
using UnityEngine;
public class Instantiate : MonoBehaviour { public Vector3[] Sizes; private float zPosition = 0.0f; private float oPosition;
private void Start()
{
for (int i = 0; i < Sizes.Length; i++){
zPosition = oPosition + zPosition;
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.localScale = Sizes[i];
cube.transform.position = new Vector3(0, 0, zPosition);
oPosition = Sizes[i].z;
Debug.Log("Placed a Cube");
}
}
}
Comment