Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by thepotatisbulle · Apr 03, 2016 at 09:46 AM · transformparenting

How do I unparent an object by scripting

How do I unparent the object once i stop holding down the mouse button?

   if (Input.GetKeyDown(KeyCode.Mouse1))
         {
             GameObject.transform.parent = gameObject.transform;
         }
         else if (Input.GetKeyUp(KeyCode.Mouse1))
         {
             GameObject.transform.parent = gameObject.transform;
         }

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

2 Replies

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

Answer by thepotatisbulle · Apr 03, 2016 at 12:32 PM

I fixed the problem by using null

   if (Input.GetKeyDown(KeyCode.Mouse1))
          {
              GameObject.transform.parent = gameObject.transform;
          }
          else if (Input.GetKeyUp(KeyCode.Mouse1))
          {
              GameObject.transform.parent = null;
          }
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 NewYorkHOK · Oct 25, 2017 at 05:43 PM 0
Share

Hey, Could you please give example of how you made this work? I have a game object that is parented by another. I would like to onClick unparent the child so that it does not follow the parent any longer.

Thanks for any help!

avatar image
3

Answer by St4Rn3Rd012 · Dec 19, 2020 at 04:08 PM

@thepotatisbulle I know I'm a little late to the party with this answer, but luckily Unity provides a function for that.

 transform.DetachChildren();

This places the responsibility on the parent gameObject to let go of all its children.

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 Smsaliou5935 · Feb 16, 2021 at 11:07 PM -1
Share
 How do you use transform.DetachChildren();? Is it just transform.DetachChildren(object, object);, or  transform.DetachChildren(object);, or something else?
avatar image Hellium Smsaliou5935 · Feb 17, 2021 at 12:06 AM 0
Share

How about reading the documentation?

https://docs.unity3d.com/ScriptReference/Transform.DetachChildren.html

avatar image larry2013z · May 03, 2021 at 01:05 AM 0
Share

This (DetachChildren) is the only reliable way I've found to detach a large number of children from a parent. The loop is not only more complicated, it does not reliably work.

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

59 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

Related Questions

Game-view and run time editor are displaying animation but the projectile is spawned as if AI was in T-pose 1 Answer

How to set the parent of an instantiated game object 1 Answer

Bug floating object when fixed to bones 0 Answers

Parenting GameObject to another animated GameObject 0 Answers

Player position relative to rotating platform [gif included] 0 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