• 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 /
This question was closed May 30, 2016 at 10:17 PM by pingponger123 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by pingponger123 · May 30, 2016 at 02:46 PM · rigidbodychild object

Disable Rigidbody After object Pickup

I am fairly new to Unity, and I am making a 2 player dodgeball game. I want the players to be able to pick up the balls that are objects with rigidbodies in the scene. I already have written the C# so that once the player is within a certain distance of the object and a key is pressed, the ball becomes a child of the player, so I need to remove the rigidbody from the ball and reposition the ball relative to the player so that the ball moves with the player. This is my code, where distBallL refers to the distance between the player and the ball (not the issue) and LPickup refers to a ball gameobject in the scene:

 if ( distBallL < 2 && Input.GetKeyDown (KeyCode.RightControl)) {
 
             LPickup.transform.parent = transform;
             LPickup.GetComponent<Rigidbody>() != enabled;
         }

This error is thrown: error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

Any help is appreciated!

Comment
Add comment · Show 6
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 ownerfate · May 30, 2016 at 06:33 PM 1
Share

you could try this:

( i use .Js ) so you may have to alter it to work.

to remove a component, you would do: Destroy(GetComponent(Rigidbody)); < i may be wrong on that, i normally don't script away components.

the best way i can think of doing this, is to disable the gravity on the rigidbody and turn off any colliders that may interfere with the movement.

hope that helps a little.

avatar image pingponger123 · May 30, 2016 at 09:20 PM 0
Share

@ownerfate Thank you for the suggestion. Javascript might be a bit of a stretch but I will try disabling the gravity. Thank you again for replying!

avatar image ownerfate pingponger123 · May 30, 2016 at 09:24 PM 1
Share

anytime, and best of luck

avatar image ownerfate pingponger123 · May 30, 2016 at 10:07 PM 1
Share

to ref. the gravity, since the Rigid body can't really be disabled,

it would be:

 GetComponent<Rigidbody>().useGravity= false;

that's from your snippet of code. ( C#) i think. see if that does anything.

avatar image pingponger123 ownerfate · May 30, 2016 at 10:15 PM 1
Share

@ownerfate This helped me discover the answer. So that it is clear for everyone, ownerfate is correct - rigidbody components CANNOT be enabled or disabled, rather, only created and destroyed. This was a very longwinded question for a simple answer - the code

 Destroy (LPickup.GetComponent<Rigidbody>());

did the trick and made the ball move with the player.

Thank you very much @ownerfate!!!

Show more comments

1 Reply

  • Sort: 
avatar image
0

Answer by pingponger123 · May 30, 2016 at 09:46 PM

For anyone else seeing this, I have identified another post which asks the same question, but is three years old. I used the following statement that the other post claims to work, but get an error which I have included below the code.

 GameObject.FindGameObjectWithTag("Ball").GetComponent<Rigidbody>().enabled = false;


error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement.

and, if I phrase it a different way,

error CS1061: Type UnityEngine.Rigidbody' does not contain a definition for enabled' and no extension method enabled' of type UnityEngine.Rigidbody' could be found.

Maybe GetComponent().enabled is deprecated?

Any help is greatly appreciated.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Prevent character equipment from clipping through walls using physics 1 Answer

How does child's rigid body affect parent's rigidbody? 1 Answer

Issue with child object collider moving through other colliders 1 Answer

How do I get the parent to follow the child's movement? 0 Answers

No Collision between Childobjects? 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