• 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 konose · Jul 11, 2017 at 06:32 PM · scripting problemscaleparentchildpositioning

setParent either shrinks my child object or blows it up... What gives?

I have a very simple script attached to a GameObject (GO1). It just calls a function that instantiates a new GameObject (GO2) and sets GO2 as a child of GO1.

For testing purposes, I have GO1 just instantiating prefab of itself. The issue is when I call GO2.transform.setParent, GO2 either shrinks in scale when I have worldPositionStays set false or gets massive if I set it to true.

The following code occurs in the original GameObject (GO1). Each child that gets instantiated runs the same code.

 GameObject  NewGrowth = (GameObject)Instantiate(Growth, transform.position, Quaternion.identity);
 NewGrowth.transform.SetParent(tf, false);


If worldPositionStays is false, each child object gets smaller and smaller with each instantiation. If true, each one gets bigger and bigger. The positions also change.

My goal is to have the child objects keep their original scale and position as before they were parented. I must be missing something here.

Any help would be greatly appreciated!

Comment
Add comment · Show 1
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 Jinkata · Jul 11, 2017 at 07:06 PM 0
Share

What is the parent's transform scale and the children's scale?

3 Replies

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

Answer by Jinkata · Jul 11, 2017 at 07:31 PM

So I think this is what's happening. I'm going to assume your base gameobject scale is something smaller than 1. So let's say it's Vector3(.5, .5, .5).

With worldPositionStays = false it adjust the child's size based on the parents size. when you set a child with a .5 scale to a parent of .5 scale you now have a child with .5 * .5 = .25. So you have a parent gameobject at half, and a child of quarter. Do that again with another child and now you have .25 * .5 = .125.

With worldPositionStays = true it tries to adjust the child to stay it's relative size. So instead it does something like this. Child = .5 scale. Parent = .5 scale. Now your scale is .5/.5 = 1 to make the child twice the size it was set to in order to overcome the half size of the parent to maintain the child's size. The next child however is .5/.5/.5 = 2 and so on.

If that's not exact then I think it's pretty close to what's happening.

Possible Solution

You could maybe overcome this by simply setting the worldPositionStays = false and manually calculating the scale of the child with something like 1/(parent scale) * (child scale) and then setting the child's localPosition to Vector3.zero.

Another Possible Solution

Another way to get around this is to have empty game objects with a scale of 1 that hold the visual object. Then each instantiation is basically just an empty gameobject of scale 1 parented to another empty gameobject of scale 1 with each one simply having it's own child with the visual component.

Comment
Add comment · Show 1 · 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 Jinkata · Jul 15, 2017 at 08:07 PM 0
Share

@konose Did you figure it out?

avatar image
1

Answer by konose · Jul 16, 2017 at 11:30 PM

Thanks @Jinkata! Your second solution worked.

It was doing exactly what you said, which doesn't seem right to me.

I still don't understand why setParent is working this way. It seems to me that worldPositionStays = true should keep the object exactly the same as it was previously, including taking into account the scale/position of the parent.

Is this how it was intended to work?

Comment
Add comment · Show 1 · 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 Jinkata · Jul 20, 2017 at 05:05 PM 0
Share

I've run into similar issues before. I'm not sure why they do it this way. The worldPositionStays = true only seems to work one level down. Anything past that and it gets all wonky again.

avatar image
0

Answer by MagyarPeter · Apr 16 at 02:09 PM

GameObject copy = Instantiate(prefabName, parentName, true);

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

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

108 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 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

Make a simple tree 1 Answer

How do i find the closest child? 1 Answer

Scale child while ignoring the parent? 2 Answers

Good way to scale parent without scaling children? 1 Answer

Parenting issue: Building and windows (scaling them as a whole entity) 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