I'm trying to learn the Unity game engine. I'm trying to develop a mechanic. Objects are created from the object pool in a fixed location. However, when I make them child object of a different object and finish the process and deactivate them, the scale values of the reconstructed objects change. Why is this happening?
private IEnumerator Create_Green_Pizzas(List pizzas) {
int pizzaCount = 0;
while (pizzaCount < pizzas.Count)
{
currentColumn ;
if (currentColumn >= columnCount)
{
currentRow ;
currentColumn = 0;
}
if (currentRow >= rowCount)
{
pizzaLevel ;
currentRow = 0;
currentColumn = 0;
}
for (int i = 0; i < pizzas.Count; i )
{
if (!pizzas[i].gameObject.activeInHierarchy)
{
pizzas[i].gameObject.SetActive(true);
pizzas[i].parent = pizzasParent;
pizzas[i].rotation = transform.rotation;
pizzas[i].position = initialSlotPosition new Vector3(((pizzas[i].lossyScale.x) * currentColumn), (pizzaLevel * pizzas[i].lossyScale.y), ((-pizzas[i].lossyScale.z) * currentRow));
pizzas[i].GetComponent<MeshRenderer>().material.color = Color.green;
_player.collectableGreenPizzas.Add(pizzas[i]);
pizzaCount ;
yield return new WaitForSeconds(0.5f);
break;
}
}
}
}
CodePudding user response:
Setting gameobject's the parent might be the cause, so instead of:
pizzas[i].parent = pizzasParent;
try instead:
pizzas[i].SetParent(pizzasParent, true);
You can find more info about this method
However you can force the GameObject to preserve its coordinates and scale using this line :
child.SetParent(parent, true);