Avoid scaling GameObject after parenting

Hello Community,

I have a GameObject (Plate) with the Scale (X=13 / Y=1 / Z=7)

and a GameObject (Item) with the Scale (X=1 / Y=0.05 / Z=1)


Now I have Script on the Plate (AttachItemScript)

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Item")
        {
            other.transform.parent = transform;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.tag == "Item")
        {
            other.gameObject.transform.parent = null;
        }
    }

The Plate is moving and the item should moved too, (OnTriggerEnter).

It’s possible, that an item can leave the plate (OnTriggerExit).


The Problem is, sometimes the Item exit the plate and the scale of the item is changing.

How can I avoid this?

@Paski-7 When looking at your code it looks like you try to set the transform of the parent of the colliding item to its parent´s transform(which includes rotation, scale and position) and then trying to nullify it when it exits the collider. It would be helpfull if you could post the purpose of the script. Then I can surely help you.

Instead of doing

other.transform.parent = transform;

do

other.transform.SetParent(transform, true);

Divide the item’s scale by the plate’s scale as it’s set as the parent.

item.transform.localScale = item.transform.localScale / plate.transform.localScale;