• 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 rohanbanerjee · Jun 17, 2015 at 07:40 PM · c#rigidbody2dhingejoint2dconnectedbody

How to set the "Connected Body" of a HingeJoint 2D using Script?

At first, I add the hingejoint 2d component to the gameobject. Then, I have a prefab, I instantiate it and want to assign its rigidbody 2d component to the connected body of the hingejoint 2d. Iam unable to do this. I read all the documentation as well as the blogs. Please help. Here's the code.

 //Boxnew is the prefab, I want its rigidbody component
 GameObject box2 = (GameObject)Instantiate (boxnew);
     
 //Now I attach hingejoint to my gameobject, works fine
 hingenew = gameObject.AddComponent<HingeJoint2D>();
 
 hingenew.GetComponent<HingeJoint2D>().connectedBody = box2.rigidbody2D;    
 //Error Above
 //Error: InvalidCastException: Cannot cast from source type to destination type.

Iam unable to set the connected body at runtime. Literally gave up.

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
0

Answer by superkolia5 · Aug 26, 2020 at 02:10 AM

box2.GetComponent();

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Object keeps moving after hit 0 Answers

Lerping on rigidbody2d.position isn't working properly, is it supposed to? 2 Answers

Rigidbody2D.IsKinematic giving object reference not set? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges