setting parent as object warps the child object

I have two cubes, cube1 and cube2. Both cubes are the same size initially.

cube1’s transform Rotation is 0,45,0 and Scale is 1,1,0.3.

cube2’s transform Rotation is 0,0,0 and Scale is 1,1,1.

If I make cube2 cube1’s child, cube2 warps (see image). I want cube2 to move with cube1 but not be warped. This doesn’t happen if cube1’s Scale is 1,1,1. How do I stop cube2 warping? I actually dynamically parent cube1 in code so would be happy with a code based solution.

After parenting:
124440-cube1cube2.png

Before Parenting:
124441-cubesbefore.png
Thanks for any help!

Hello

I’ve encountered this problem also. This happens because child gameobjects inherit all of their parents’ scales and rotations. The simple solution is to just place both your gameobjects under an empty parent gameobject with the rotation (0, 0 ,0) and scale of (1, 1, 1) and then rotate and scale them individually.
To make your cube2 follow cube1 you will have to move the empty parent gameobject and both your cubes will follow. Rotating the parent should rotate all children (they will all rotate around a common pivot point, as if they were all one object) but scaling the parent will result in weird warps as you already noticed.

As for a code solution:

var prarent = Instantiate(parentPrefab); // an empty gameobject prefab with nothing except a transform
var cube1 = Instatiate(cubePrefab); // your cube prefab
var cube2 = Instatiate(cubePrefab);
cube1.transform.parent = parent.transform;
cube2.transform.parent = parent.transform;
cube1.transform.rotation = Quaternion.Euler(0, 45, 0);
cube2.transform.rotation = Quaternion.Euler(0, 0, 0);
cube1.transform.localScale = new Vector3(1, 1, 0.3);
cube2.transform.localScale = new Vector3(1, 1, 1);

something like that. You could probably use one of the Instatiate overloads to make it nicer looking.

Good day.

This makes no sense… If you ahve 2 objects, with no parents or childs, and make on child of the other, you will see no changes in the scene, objects will not move or change anything…

This happens during play? Then its a script.