• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by tammyhuang · Jun 17, 2016 at 11:00 PM · scaleprefabsruntime

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!

Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SarperS · Jun 17, 2016 at 11:38 PM 0
Share

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.

Show more comments
avatar image tammyhuang · Jun 18, 2016 at 05:43 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tammyhuang · Jun 18, 2016 at 12:32 AM 0
Share

Cool! The scale problem solved. How about the position? I tried this after parent but still doesn't work:

 girlBody.transform.position = posGirlBody;
avatar image SarperS tammyhuang · Jun 18, 2016 at 12:37 AM 0
Share

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

avatar image tammyhuang SarperS · Jun 18, 2016 at 12:51 AM 0
Share

Problem solved!!! You are super! Thanks a million!!!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

60 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Download and use an asset bundle in arcore 0 Answers

Scaling Rigged Character at Runtime Through Script? 0 Answers

Architecture advice on how to cycle a prefab's characteristics at runtime? 0 Answers

LocalScale is not taking effect at runtime 1 Answer

How to change the render mode of a material in runtime? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges