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
0
Question by Jean-Fabre · Aug 18, 2010 at 12:17 PM · gameobjectrigidbodyconverttypebce0022

convert GameObject to Rigidbody type

Hi,

When I find a game Object like this:

GameObject.Find("physics_body")

If I try to assign it to a variable that is typed as Rigidbody, it fails.

There is a rigidbody component applied to this model yes, and If I use the inspector it all works fine.

I get the following:

BCE0022: Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Rigidbody'.

Note that it works very well within the inspector, but how can I do the same via script? there is a conversion going on somewhere but I fail to find how to do that within a script.

How should I go about it?

thanks,

Jean

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

Answer by jonas-echterhoff · Aug 19, 2010 at 09:52 AM

A GameObject and a Rigidbody are two entirely different types, so you cannot assign one to the other. As you find out, you have to get the Rigidbody component from the GameObject instead, ie:

joint.connectedBody = GameObject.Find("physics_body").rigidbody;

The reason you can do this in the inspector is that the inspector is smart, and if you drag a GameObject onto a component reference, it will automatically search the GameObject for matching components to assign. As you cannot drag a component itself, this is also needed to have an easy way to assign component references in the editor.

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 Jean-Fabre · Aug 18, 2010 at 12:49 PM

Found it after messing around desperately

you actually pass the component reference itself.

something like this:

<joint>.connectedBody = <GameObject>.GetComponent(Rigidbody);

Simple. Maybe it's in the doc somewhere, but I would say it would deserve a bit more explanation or examples on this.

Oddly enough, passing the right value solved other problems that at first seemed unrelated, I was getting errors about the connectedBody function itself, not the fact that the value passed was of the wrong type. see http://answers.unity3d.com/questions/18378/connectedbody-error-when-used-in-script

Jean

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 JuanseCoello · Apr 10, 2014 at 10:48 PM 0
Share

How could I use this in C#?

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

1 Person is following this question.

avatar image

Related Questions

Convert String to GameObject[] type Array 2 Answers

How could I convert GameObject to Rigidbody in C#? 0 Answers

Game objects sliding when frozen z-axis? 0 Answers

Creating a teleportation gun 1 Answer

How to tilt the gameobject based of Input.Acceleration? 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