Why my prefab's Scale is changed to 0 at runtime?
Here's my code:
Vector3 posGirlBody = new Vector3 (0, 0, 0);
GameObject girlBody = Instantiate (girlPrefabs [Random.Range (0, girlPrefabs.Length)], posGirlBody, Quaternion.identity) as GameObject;
var crtScale = girlBody.transform.localScale;
Debug.Log ("Current Scale is: "+crtScale);
girlBody.transform.parent = transform;
The Debug shows: Current Scale is: (1.0, 1.0, 1.0), but in my Inspector, when I click on the prefab (clone), the Scale shows (0, 0, 0), and I can't see the prefab in my game view, so it was in deed reset to 0 at runtime. How to solve the problem? Please help!
Is it an animated model? Are the animations separate model files? If so is their import scale match the main model's import scale? Disable the animation component if there is one to test it in runtime.
Also ins$$anonymous$$d of creating a new vector for position 0 you can always use Vector3.zero in your Instantiate call.
It is not an animated model. The prefab is just an image I dragged on canvas, and the prefab's scale is (1, 1, 1).
I actually want the prefab to locate in its parent panel's top left corner.
And my position is reset to left top corner of the screen and shown the same information in Inspector as well, despite I have the Vector3 set to where it should be and the DebugLog showed the correct position. See my modified code:
Vector3 posGirlBody = new Vector3 ( widthOffsetForRatio + xOffset, heightOffsetForRatio + yOffset, 0);
Debug.Log ("Girls's Position is"+posGirlBody);
GameObject girlBody = Instantiate (girlPrefabs [Random.Range (0, girlPrefabs.Length)], posGirlBody, Quaternion.identity) as GameObject;
girlBody.transform.position = posGirlBody;
var crtScale = girlBody.transform.localScale;
Debug.Log ("Current Scale is: "+crtScale);
girlBody.transform.parent = transform;
The Debug Log shows: Girls's Position is(120.0, -475.0, 0.0) Current Scale is: (1.0, 1.0, 1.0)
Weird problem! :-( Never had this before. I wrote the same way as I did in my previous project which worked, but this time they don't work.
Answer by SarperS · Jun 18, 2016 at 12:25 AM
Okay so it's a UI element, in Unity, when you change the parent of a transform, it must take into account all the parent transform scale and orientations into account. In this case, the described problem lies in the top-most parent which is "Canvas" which has a scale very close to zero (and probably rounds to 0 on parenting).
Solution is rather simple. After you parent it, set it's local scale to 1 :)
girlBody.transform.parent = transform;
girlBody.transform.localScale = Vector3.one;
Cool! The scale problem solved. How about the position? I tried this after parent but still doesn't work:
girlBody.transform.position = posGirlBody;
Get rid of both those lines and use below method if you want the child to retain it's pre-parenting position and scale.
girlBody.transform.SetParent(transform, false);
Refer to the scripting reference for more details https://docs.unity3d.com/ScriptReference/Transform.SetParent.html
Problem solved!!! You are super! Thanks a million!!!