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 /
avatar image
1
Question by Jordan Miller · May 05, 2010 at 06:36 PM · rigidbodydisableenableremovemissingfieldexception

Can't remove Rigidbody from transform in script?

I'm trying to remove a rigidbody from a transform in my script and I get this error: "MissingFieldException: Field 'UnityEngine.Rigidbody.enabled' not found." Here's my code:

transform.GetComponent(Rigidbody).enabled = false; 

is there a way to remove it, or disable it? this same code works when I replace "Rigidbody" with the name of a script that's attached to the transform.

Ps. I also tried:

transform.GetComponent("Rigidbody").enabled = false; 

and

transform.GetComponent(typeof(Rigidbody)).enabled = false; 

but I get the same error.

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

8 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by Jordan Miller · May 05, 2010 at 06:58 PM

there we go, I found this in the scripting manual, under addComponent():

"Note that there is no RemoveComponent(), to remove a component, use Object.Destroy."

so the code is simply this:

// Removes the rigidbody from the game object
Destroy (rigidbody);

that was easy.

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
1

Answer by Max Kaufmann · May 05, 2010 at 06:42 PM

You're trying to operate on a vanilla "UnityEngine.Object" reference. Use the generic version to get a specialized reference:

transform.GetComponent<Rigidbody>().enabled = false;
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 Jordan Miller · May 05, 2010 at 06:50 PM 0
Share

ok, i tried that but it didn't work either. (I'm doing this in JavaScript by the way) I still get that error along with the following: "expecting an identifier, found ')'." and "expecting ), found '.'" and "';' expected. Insert a semicolon at the end." is your code for C# or something?

avatar image
1

Answer by Mike 3 · May 05, 2010 at 06:50 PM

or, more simply, if rigidbody had an enabled: rigidbody.enabled = false;

you don't need to use transform to get at GetComponent, and rigidbody is the property to get at the rigidbody more easily

but anyway - rigidbody doesn't have an enabled - you need to either remove the rigidbody, set the gameobject inactive or set isKinematic = true

Comment
Add comment · Show 5 · 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 Jordan Miller · May 05, 2010 at 06:53 PM 0
Share

ok, I tried that too. and I get this error, "'enabled' is not a member of 'UnityEngine.Rigidbody'." and I also get the error I was originally getting.

avatar image Jordan Miller · May 05, 2010 at 06:54 PM 0
Share

oh, so how do I remove it?

avatar image Jordan Miller · May 05, 2010 at 06:55 PM 0
Share

I mean in script?

avatar image Mike 3 · May 05, 2010 at 06:57 PM 0
Share

Destroy(rigidbody);

avatar image Jordan Miller · May 05, 2010 at 07:00 PM 0
Share

thanks mike, I can't believe how simple that turned out to be.

avatar image
1

Answer by cob · May 11, 2010 at 06:18 PM

In C#

UnityEngine.Object.Destroy(gameobjectinstance.GetComponent<RigidBody>());

If you want to change the prefab you have to load it, save it three times and reload the asset database at the start and the end of the script. I hate black magic! Hope unity version 3 fixes some of these issues.

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
1

Answer by nick_taras · Nov 29, 2012 at 11:33 AM

I hope this is what you need - You can remove the physics from the game object if you enable the rigidbody's isKinematic parameter in the script: (Javascript)

rigidbody.isKinematic = false; // enable physics rigidbody.isKinematic = true; // disable physics

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
  • 1
  • 2
  • ›

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Enable or Disable Rigid Body at Runtime? 9 Answers

Rigidbody velocity limiter 0 Answers

Disable Rigidbody Function Start 1 Answer

How do I disable all rigidbodies and colliders in a scene in unity? 2 Answers

Temporarily removing rigidbody and configurable joint and then replacing them 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