Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 /
avatar image
28
Question by gdbjohnson · Jan 04, 2015 at 04:55 PM · scalelayout

Why is Instantiated object's scale changing?

I have very simple code to instantiate a prefab object that is set like so many tutorial examples I am currently following. I am finding however, that my object below has a scale changed to (2, 2, 2), instead of (1, 1, 1). I have to have that last bit of code to fix it, setting the localScale. But, I don't understand why I need to do this.

itemListContainer is a VerticalLayout container. Is the layout script setting my object's below with this scale? Where is it coming from?

  public void LoadItems(){
     Vector3 position = itemListContainer.transform.position;
     Quaternion rotation = itemListContainer.transform.rotation;
             
     foreach (Transform child in itemListContainer.transform) {
         GameObject.Destroy(child.gameObject);
     }
     
     foreach (GameSpriteData o in spriteList) {

         GameObject itm = (GameObject) Instantiate(storeItemPrefab, position, rotation);
         itm.transform.parent = itemListContainer.transform;
         //itm.transform.localScale = Vector3.one;
         
     }
 }

Comment
Add comment · Show 6
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 Mmmpies · Jan 04, 2015 at 06:06 PM 0
Share

What happens if you just drag the prefab into the scene manually before hitting play. Is the scale of the prefab twice the size of say a cube?

avatar image gdbjohnson · Jan 04, 2015 at 07:22 PM 0
Share

The scale is 1, and same for a cube, when I add it manually. Totally weird.

avatar image Mmmpies · Jan 04, 2015 at 07:29 PM 0
Share

Well that's my idea gone then don't know if I can help but can you post some screen shots of whats happening even if I can't help someone else might with more info.

avatar image gdbjohnson · Jan 04, 2015 at 07:35 PM 2
Share

I figured it out. All of this is being done on a Canvas, since this is all UI stuff I'm working on, my Canvas Scaler is causing the change. I have it scaling to screen size. Perhaps the 2 scale being applied is compensating for the Canvas Scaler?

avatar image Mmmpies · Jan 04, 2015 at 07:38 PM 0
Share

Well done and thumbs up for posting the solution :¬)

Show more comments

2 Replies

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

Answer by gdbjohnson · Jan 04, 2015 at 07:42 PM

And, I found the solution. "Consider using the SetParent method instead, with the worldPositionStays argument set to false. This will retain local orientation and scale rather than world orientation and scale, which can prevent common UI scaling issues." http://docs.unity3d.com/ScriptReference/Transform.SetParent.html

Comment
Add comment · Show 3 · 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 ksakins · Dec 09, 2016 at 06:19 AM 2
Share

Thumbs up... I actually was not having this issue until I upgraded from Unity 5.4 to Unity 5.5. I had a Text (RectTransform) and was calling SetParent, placing the UI element under another UI element inside the Canvas. It was something like this: text.transform.SetParent(parent.transform).

Prior to upgrading this worked fine, however specifying the "worldPositionStays" argument to false did the trick. $$anonymous$$aybe the Unity devs set this value to true by default in the latest update?

avatar image rmail2006 · Sep 05, 2017 at 10:04 PM 1
Share

I know it's an old answer but just for more info.

I used this and still had an issue, figured out the scale was changing on the RectTransform so made sure to set that to 1,1,1 after SetParent. Only showed when the screen size changed.

avatar image SimRuJ · May 23 at 02:10 PM 0
Share

The new parent of my instantiated GO already uses a scale of (1,1,1) but the new GO still ended up with weird values (11.xx each) and also a completely wrong position. This fixed it! If you want to also move the new GO, do that before setting the parent! If I do it afterwards, the scale and position go back to the same wrong values.

avatar image
5

Answer by dacaramo · Jun 20, 2020 at 12:52 AM

Just use the Instantiate() method overload that takes as third parameter the worlPositionStays boolean, instead of using another weird method

Comment
Add comment · Show 2 · 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 madkar3000 · Aug 19, 2020 at 12:58 PM 0
Share

Thanks, that helped

avatar image CrandellWS · Jun 26, 2021 at 05:40 PM 0
Share

I honestly did not think that would do it but it in fact stop the auto scaling issue I was having. Thanks for the help.

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.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Scaling Canvas and Layout Group 1 Answer

how to make grid of buttons always fit parent? 0 Answers

Layout error after crash has effected canvas scaling 1 Answer

Newbiew question: how to size objects? 1 Answer

Layout Groups Do Not Take Scale Into Account 1 Answer


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