Hi I have this code to generate 4 prefabs initially but the middlefloors can grow in number:
public Transform bottomFloorClose;
public Transform middleFloor01Close;
public Transform middleFloor02Close;
public Transform topFloorClose;
public Transform TowerClose;
public int numberOfFloors = 4;
private float positionXClose = 0.25f;
private float positionYClose = -11.11f;
private const float floorHeigthClose = 13.45f;
void Start()
{
GenerateTower(numberOfFloors);
}
private void GenerateTower(int numberOfFloors)
{
TowerClose.transform.localScale = new Vector3(0.78f, 0.78f, 0F);
int numberOfMiddleFloors = numberOfFloors - 2;
Transform bottomFloorObj = Instantiate(bottomFloorClose, new Vector3(positionXClose, positionYClose, 0f), Quaternion.identity);
bottomFloorObj.transform.SetParent(gameObject.transform, false);
positionYClose += floorHeigthClose;
List<Transform> floorList = new List<Transform> { middleFloor01Close, middleFloor02Close };
Random random = new Random();
for (int i = 0; i < numberOfMiddleFloors; i++)
{
int index = random.Next(floorList.Count);
Transform middleFloorObj = Instantiate(floorList[index], new Vector3(positionXClose, positionYClose, 0f), Quaternion.identity);
middleFloorObj.transform.SetParent(gameObject.transform, false);
positionYClose += floorHeigthClose;
}
Transform topFloorObj = Instantiate(topFloorClose, new Vector3(positionXClose, positionYClose, 0f), Quaternion.identity);
topFloorObj.transform.SetParent(gameObject.transform, false);
}
}
Their order is not correct, I need to have the bottomfloor like at z=0 and middlefloor1 and middlefloor2 at z=-10 and z=-20 so that topfloor will be z=-30 to be in the correct order so the sprites fit well I tried to changing their position in the inspector didn’t work. Tried in the GenerateTower script via transform.position but didn’t work.
The overall position of the tower composed by the different sprites (bottomfloor, middelfloors etc) is correct
The position of individual floors are correct in X and Y axis, not on Z
This might seem confusing but the wrong thing/the real problem is that the bottomfloor, middlefloors and topfloor don’t have the Z position because they need to be in a certain order of depth.
Imagine a puzzle, you can’t put A on top of B because only B fits on top of A so you have to put A with a lower value of Z so B fits on top of it. It’s the same with my gameobjects
I didn’t pick up on the fact it was the Z axis that was wrong. In that case, your problem lies in these bits of code Vector3(positionXClose, positionYClose, 0f). You are setting the X and Y axis values but the Z is always zero.
At least, I can’t see anywhere in your code that is setting the Z component.
So do you have another piece of code somewhere that is resetting the z to be zero?
Do you mean, you move them but they get reset? Or are they actually greyed out in the editor showing that they cannot be moved at all? What about moving them along X and Y - does that work?
I think the problem is here TowerFar.transform.position = new Vector3(15, 0, 0) or here Transform bottomFloorObjFar = Instantiate(bottomFloorFar, new Vector3(positionXFar, positionYFar, 0f), Quaternion.identity) because I’m setting the Z at 0 and then I can’t change the prefab Z order…
In the inspector they move in X and Y but not in Z, it’s like locked on Z