Mehmet Koman
Mehmet Koman

Reputation: 11

Why does the scale value of the object change?

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<Transform> 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;
            }
        }
    }
}

Upvotes: 1

Views: 261

Answers (2)

Tib&#232;re B.
Tib&#232;re B.

Reputation: 1191

If a GameObject has a parent, they become part of the parent's referential, their transform is now a local transform that depends on the parent's transform.

This means that if a child transform has a scale of (1, 1, 1), has the same scale as its parent.

Here's a small clip that illustrates this, two sphere that are the same size while they both have a different scale.

Illustration of scale

However you can force the GameObject to preserve its coordinates and scale using this line :

child.SetParent(parent, true);

Upvotes: 1

INLD
INLD

Reputation: 64

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 Here

Upvotes: 3

Related Questions