Transform.parent changes rotation/position 2D

Hi I am creating many 2D objects and I need them to be parent of the main Object_Spawner let`s call it so the hierarchy is clean.

GameObject planet = Instantiate(planet_GameObject, pos, Quaternion.identity) as GameObject;
planet.transform.parent = gameObject.transform;

But after creating those objects (Sprites) I don`t see them. They change their scale to 0,0,0 after I transform/attach them to their parent.

Please do you know how to fix this?

Your problem is the 0 you set for your ‘z’ value in the local scale of the parent object. Change it to 1.0 (or just about any positive value), and your problem will go away.

When we make something a child, it preserves it original size. Since localScale is relative, the localScale of the child is divided by the localScale of the parent. Say we had a localScale.z value of 2.0 for a potential parent. And a localScale.z value of 5.0 for the object that is about to become the child. After making it a child, the ‘z’ value would be 2.5…that is 5.0 / 2.0 = 2.5. Now if we use 0.0 for the ‘z’ value for the parent, we get 5.0 / 0.0 == divide by zero error. Unity handles this situation by setting x,y,z of the localScale to 0.0. The result is an object that cannot be seen. You can duplicate this issue by just dragging and dropping object in the Inspector.

You could store the previous position before changing the parent:

Vector3 previousPos = planet.transform.position;
planet.transform.parent = transform;
planet.transform.position = previousPos;

Side-note: you don’t need the .transform part if the ‘planet’ variable is already Transform.