Setting instantiated prefab to child of game object while preserving raw prefab transform values

Hello,

I’m trying to instantiate a prefab and set it as a child of another game object, so that the prefab’s transform will be relative to the parent’s.

However doing the following:

map = (GameObject)Instantiate (Resources.Load ("somefile"));
map.transform.SetParent(gameBox.transform);

The prefab object’s own transform values get set to the inverse of the parent’s transform, losing it’s original values.

So if the prefab pos was (0, 0, 90), and the parents pos is (1, 1, 90), after setting the parent transform, the prefab pos becomes (-1, -1, 0).

I can reset the values myself by hard coding them in after setting the transform, but being as its a prefab, it kind of loses the point of storing all those magic numbers for me.

What am I missing?

Thanks,
-Ryan

Perhaps you want to specify the ‘worldPositionStays’ parameter in the SetParent method.

“If true, the parent-relative position, scale and rotation is modified such that the object keeps the same world space position, rotation and scale as before.”

See: Unity - Scripting API: Transform.SetParent

I feel like a dunce. Thanks.