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
2
Question by soundwav_ · Dec 13, 2013 at 03:17 AM · c#gameobjectparentchild

"Center On Children" programmatically

Hi, Is there a way to use the "Center On Children" command (found under the GameObject menu) from a C# script?

I know I can figure out the centered position, and un-parent all children while I move the parent itself, and maybe "Center On Children" does exactly that, I dunno, but maybe it does something more efficient behind the scenes.

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
9
Best Answer

Answer by Bunny83 · Dec 13, 2013 at 03:52 AM

All this menu item does is this:

 //C#
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 
 public static class GO_Extensions
 {
     public static void CenterOnChildred(this Transform aParent)
     {
         var childs = aParent.Cast<Transform>().ToList();
         var pos = Vector3.zero;
         foreach(var C in childs)
         {
             pos += C.position;
             C.parent = null;
         }
         pos /= childs.Count;
         aParent.position = pos;
         foreach(var C in childs)
             C.parent = aParent;
     }    
 }

With this class somewhere in your project you can simply use it like this on any transform:

     transform.CenterOnChildred();


Of course it's possible to do it without unparenting the childs, but if you have to consider scaling and rotation of the parent which is quite nasty. This is the easiest way.

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 soundwav_ · Dec 13, 2013 at 08:23 AM 0
Share

Thanks! I was wondering how to get a copy of the children, but this code throws me 2 errors (after creating a new C# script named 'GO_Extensions' and replaced the existing code with the one above):

Cannot convert type UnityEngine.Transform' to > System.Collections.Generic.IEnumerable' via a built-in conversion

Operator /=' cannot be applied to > operands of type UnityEngine.Vector3' and `method group'

I assume I could do a foreach loop of Transforms on the parent's transform, but since it's not a copy, it would probably get changed during the iteration.. and I have no idea on the second error :) Thanks

avatar image Bunny83 · Dec 13, 2013 at 10:01 AM 1
Share

Yes, sorry ;) I didn't test this yesterday. I always forget that you can't directly cast the IEnumerable to the generic IEnumerable. All you have to change is this:

 var childs = aParent.Cast<Transform>().ToList();

I'll edit my answer

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

17 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

Related Questions

Accessing variable from parent 1 Answer

Can I limit a .Find to the Parents and Child and not the Scene? 3 Answers

How can I listen for a function being called from a parent class? 1 Answer

Make a simple tree 1 Answer

Instantiated GameObject gets spawned as a child 2 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