• 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
1
Question by Duke Euphoria · Oct 11, 2012 at 11:42 AM · transformscaleparentscaling

How to preserve size of children when changing scale of parent

Hi all

Got a bit of a conundrum. I have some scaled objects w$$anonymous$$ch I would like to be able to shrink, revealing wit$$anonymous$$n them other objects. Consider it a rock with gems inside I want to shrink the rock to show the gems.

After having a bit of a read through these pages it seemed the simplest t$$anonymous$$ng to do in order to preserve the scale of the gems would be to un-parent the gems, change the scale of the rock, then re-parent the gems. Somet$$anonymous$$ng like t$$anonymous$$s.

 void NewRockSize(float Size)
 {
     gem.transform.parent=null;
 
     rock.transform.localscale=new Vector3(Size,Size,Size);
 
     gem.transform.parent=rock.transform;    
 }

In direct contradiction to the doc page Here the scale of the gem is not recalculated and, as it inherits the scale of its parent rock it appears the wrong size.

Answer me these questions three...
Am I doing somet$$anonymous$$ng wrong ?
Are the Docs wrong ?
Is there a known way to do t$$anonymous$$s right ?

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

5 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Jashengmato · Mar 23, 2013 at 06:23 PM

Have you solved t$$anonymous$$s problem yet? If not, try t$$anonymous$$s.

 float rockOriginalScale = rock.transform.localScale.x ; 
 
 void Resize(float size)   
 { 
      float scale = rock.transform.localScale.x / size ; 
      float gemOrg = gem.transform.localScale.x ; 
      rock.transform.localScale = new Vector3(size, size, size) ; 
      gem.transform.localScale = new Vector3(gemOrg*scale, gemOrg*scale, gemOrg*scale) ;
 }


T$$anonymous$$s should work!

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
avatar image
0

Answer by rajat · Jul 25, 2013 at 10:40 AM

removing and re-adding c$$anonymous$$ldren seems to work fine for me:

 private void ChangeParentScale(Transform parent,Vector3 scale)
 {
     List<Transform> c$$anonymous$$ldren= new List<Transform>();
     foreach(Transform c$$anonymous$$ld in parent) {
         c$$anonymous$$ld.parent = null;
         c$$anonymous$$ldren.Add(c$$anonymous$$ld);
     }
     parent.localScale = scale;
     foreach(Transform c$$anonymous$$ld in c$$anonymous$$ldren) c$$anonymous$$ld.parent = parent;    
 }
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
avatar image
0

Answer by springwater · Oct 12, 2015 at 05:40 PM

here's my solution :P

Create an object/variable of the scale you want.. do not parent it.. its a template.. then during scaling operations check to see if your c$$anonymous$$ld object has exceeded one of the template's vector3 lengths.. if so.. scale the c$$anonymous$$ld object back to template size ^^

   var c$$anonymous$$ld: GameObject;     //c$$anonymous$$ld of some other object in another script perhaps. 
     var atemplate :GameObject;
                
          Update(){  
     if(C$$anonymous$$ld.lossyScale.x!=atemplate.lossyScale.x)
     c$$anonymous$$ld.transform.localScale=atemplate.transform.localScale;
    }
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 springwater · Oct 13, 2015 at 01:23 AM 0
Share
avatar image
0

Answer by rockyourteeth · Oct 13, 2015 at 02:58 AM

You could make an empty parent object, and then make your two object siblings instead of parent/c$$anonymous$$ld. In other words, you have a parent with two c$$anonymous$$ldren, one that you want to scale, and one that you don't. Now you can scale the one without affecting the other.

You can put your script on the parent, and the mesh, or whatever it is you're scaling, on the c$$anonymous$$ld.

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
avatar image
2

Answer by Rispat-Momit · Sep 07, 2018 at 08:23 PM

Hi there! I figure out t$$anonymous$$s solution :D

Just set t$$anonymous$$s script to your c$$anonymous$$ld ;) It also works in Edit Mode so that you can set up the scale you need.

At the FixeScale set up the size of your c$$anonymous$$ld you need and at the parent set your parent model.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [ExecuteInEditMode]
 public class FixedScale : MonoBehaviour {
 
  public float FixeScale =1 ;
     public GameObject parent;
     
     // Update is called once per frame
     void Update () {
         transform.localScale = new Vector3 (FixeScale/parent.transform.localScale.x,FixeScale/parent.transform.localScale.y,FixeScale/parent.transform.localScale.z);
 
             }
         }


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 normand · Oct 17, 2018 at 04:44 AM 0
Share
avatar image normand normand · Oct 17, 2018 at 10:59 AM 0
Share
avatar image Amphelion · Jul 23, 2020 at 10:57 AM 0
Share

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

18 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

Related Questions

Object scale/rotation changes when parented to flipped object 0 Answers

Rescaling goes wrong? 0 Answers

Avoid scaling GameObject after parenting 2 Answers

How can I attach a gameobject to another without changing its scale. 1 Answer

Good way to scale parent without scaling children? 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