1
votes

This is driving me absolutely batty! Here is what is happening and I just don't understand

In the editor my parent object has a scale of 100,100,100

the prefab has a transform as follows:

Position: 0, -1.939, 0 Rotation: 0, 0, 0 Scale: 0.3946165, 0.3946165, 0.3946165

If I drag this to the parent it looks perfect, exactly as I want.

I export the prefab as an AssetBundle and import at runtime like this:

currentPerson = Instantiate(bundle.mainAsset,new Vector3(0, -1.939f, 0), Quaternion.identity) as GameObject;

currentPerson.transform.localScale = new Vector3(300, 300, 300);

currentPerson.transform.parent = GameObject.Find("PeopleImageTarget").transform;

Debug.Log(currentPerson.transform.position.x + ", " + currentPerson.transform.position.y + ", " + currentPerson.transform.position.z);

Debug.Log(currentPerson.transform.rotation.x + ", " + currentPerson.transform.rotation.y + ", " + currentPerson.transform.rotation.z);

Debug.Log (currentPerson.transform.localScale.x + ", " + currentPerson.transform.localScale.y + ", " + currentPerson.transform.localScale.z);

Now the child object appears but it is still tiny and where I have the scale set to 300 to try to increase its size it reports 3 in the debug log

This is running as an exported to IOS app maying use of the augmented reality tool from Vuforia hence the parent scale being 100

If I include the child in the app and simply set active on or off the child appears perfect it is only going tint when instantiating which is a feature I need to import models at runtime....

3
try setting the localScale after assigning transform to the parentLearnCocos2D
Try using transform.SetParent and take note of the worldPositionStays parameter: docs.unity3d.com/ScriptReference/Transform.SetParent.htmlChris McFarland
Chris, that fixed it WOOT :-) can you add your comment as answer so I can mark up?user7865437

3 Answers

1
votes

When you reparent an object, its world-scale stays the same. That means its local scale changes. So if you know what you want its local scale to be after it's reparented, you should set its local scale after reparenting.

0
votes

You should set parrent first, and then set the scale.

Switch this lines:

currentPerson.transform.localScale = new Vector3(300, 300, 300);
currentPerson.transform.parent = GameObject.Find("PeopleImageTarget").transform;

try:

currentPerson.transform.parent = GameObject.Find("PeopleImageTarget").transform;
currentPerson.transform.localScale = new Vector3(300, 300, 300);
0
votes

To retain scale (or not) when changing parents, use Transform.SetParent and set the worldPositionStays parameter as desired:

Transform targetTransform = GameObject.Find("PeopleImageTarget").transform;
currentPerson.transform.SetParent(targetTransform, true);

Note that this requires Unity 4.6 or higher.