ace2df
1
I’m trying to get these objects in a line connected to each other but it’s not working out too well.
void StackUp()
{
tempHolder = new GameObject("Holder").transform;
tempHolder.SetParent(boardHolder);
GameObject instance;
for (int y = startPos; y < endPos; y++)
{
GameObject toInstantiate = lineTiles[Random.Range(0, lineTiles.Length)];
instance = Instantiate(toInstantiate, new Vector3 (0, currentPos, 0f), Quaternion.identity) as GameObject;
currentPos += (float)instance.GetComponent<Collider2D>().bounds.size.y;
instance.transform.SetParent(tempHolder);
}
}
The objects in lineTiles are different sizes in height and width.
I can’t seem to get them to stay connected though. I think it would have to do with currentPos but no idea what I’m doing wrong to get it like this:

There are also some objects hidden under each other:
You should consider each gameobject size and center separately:
for (int y = startPos; y < endPos; y++)
{
float currentPos = .0f;
GameObject toInstantiate = lineTiles[Random.Range(0, lineTiles.Length)];
instance = Instantiate(toInstantiate, new Vector3 (0, currentPos, 0f), Quaternion.identity) as GameObject;
float size = (float)instance.GetComponent<Collider2D>().bounds.size.y;
instance.transform.position = new Vector3(0,currentPos + size/2,0); // center position of the gameobject
currentPos += size; //up position of the gameobject
instance.transform.SetParent(tempHolder);
}