Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by JJks · Nov 26, 2013 at 10:31 AM · transform.changing shader

How to instantiate gameobject as a child without scaling

Hi,

I tried to instantiate a gameobject and registerd as some object's child. My code is like below. As you can see I'm always store localscale value before registering as a child because localscale is changed after registering. My code is working but it's a little troublesome because it is my basic method in my application.

Does anyone know other better method?

 var someObj = Instantiate(Resources.Load(prefabName)) as GameObject;
 var localScale = someObj.transform.localScale;
 someObj.transform.parent = transform; // someObj.transform.localScale is changed!
 someObj.transform.localScale = localScale;


thanks,

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Dover8 · Nov 26, 2013 at 11:57 AM

This is how localScale works, it gives you the scale in relation to the parent object. If you remove/comment your last line then the object will be parented and still be the same size it was before parenting, but the localScale will be it's scale in relation to the parent.

The best method for instantiating prefabs is to make sure that the prefab root has a scale of (1,1,1) in respect to the parent object that you will instantiate it to. Even if you make an empty game object in the editor to be (1,1,1) as the child, and then make this as the parent of your prefab and apply this as your new prefab. Then at run time you know that you can instantiate and call:

 somObj.transform.localScale = Vector3.one; 

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 Bunny83 · Nov 26, 2013 at 11:59 AM 0
Share

Right, if you need deeper hierarchies it'S best to build those hierarchies with empty gameobjects and just add the actual objects as child. The "visual" objects can be scaled as you which but the hierarchy isn't affected.

avatar image JJks · Feb 27, 2014 at 05:30 AM 0
Share

Thank you for your comments. I understand the manner of localScalie. But I have actually have some cases which I want to force child's gameObject to set its original size. In such cases, I prepared function like below.

 var localScale = child.transform.localScale; // store original localScale value
 child.transform.parent = parent;
 child.transform.localScale = localScale; // force set
avatar image whydoidoit · Feb 27, 2014 at 05:33 AM 0
Share

But that specifically does not keep it's original size, it scales it based on the parent as @Dover8 mentions. If you don't put that last line, it will retain it's original size presu$$anonymous$$g it's parents have uniform scaling if they do not, all sorts of weirdness may ensue.

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

19 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

Related Questions

Unity takes Forever (15 minutes) to apply inspector settings 0 Answers

Change controls to vehicle controls script? 0 Answers

scaling (Resizing) orthographic camera in 2D mode 3 Answers

Stop a enemy by pointing a light at it. 4 Answers

Limit rotate object? 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