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
0
Question by Nick_Artemov · Oct 07, 2015 at 07:04 PM · gameobjectrigidbodytypecast

Convert (Typecast) Rigidbody to GameObject

Unity 5.1.2f

I have Scene with Main Camera, object with rigidbody component and empty game object. Main Camera has a script attached to it. In public references to Game Object and Rigidbody are getted by drag-and-drop from Hierarchy View to Inspector.

No matter how i try to cast Rigidbody to GameObject I've got error whenever it's as-cast, c-cast or imlicit conversion.

     public Rigidbody rocket;
     public GameObject gameObject;
 
     void Start()
     {
         if(rocket != null && gameObject != null)
         gameObject = rocket;
     }

error CS0029: Cannot implicitly convert type UnityEngine.Rigidbody' to UnityEngine.GameObject'

If I try as-cast or c-cast I've got other errors. error CS0039: Cannot convert type UnityEngine.Rigidbody' to UnityEngine.GameObject' via a built-in conversion error CS0030: Cannot convert type UnityEngine.Rigidbody' to UnityEngine.GameObject'

What's the matter??

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

Answer by Dave-Carlile · Oct 07, 2015 at 07:08 PM

A Rigidbody is not a GameObject so you cannot cast to it.

If you want to access the game object that a component is attached to then you can use the gameObject property that exists on every component.

 GameObject go = rocket.gameObject;

Or if you want to assign a component that is attached to a gameobject...

 Rigidbody rocket = gameObject.GetComponent<Rigidbody>();


The Unity editor does some of these things behind the scenes when you for example drag a gameobject to a property that wants a component.

What are you trying to do exactly?

Comment
Add comment · Show 2 · 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 Nick_Artemov · Oct 13, 2015 at 08:55 PM 0
Share

A Rigidbody is not a GameObject so you cannot cast to it.

But aren't they both value types? Also as I remember I can cast from GameObject to Rigidbody. And I can cast from Rigidbody to Transform but a Rigidbody is not a Transform.

What are you trying to do exactly?

Nothing particular. I just found this error while working and know curious about why I can typecast from one Component type to other, or to an Object type but cannot cast to a GameObject type while I can cast from a GameObject type to a various component types.
avatar image Dave-Carlile Nick_Artemov · Oct 13, 2015 at 11:40 PM 1
Share

Rigidbody and GameObject, like all classes, are reference types, not value types.

When you say "typecast" what do you mean? You can't typecast a GameObject to a Rigidbody. You will get the very error that you list as an error. You also can't cast a Rigidbody to a Transform - you will get the same error.

All components (including $$anonymous$$onobehaviours) descend from the common class Component. You can typecast any component descendant to Component, and you can typecast a Component to a descendant class. The former will always succeed because a Component descendent is a component. But the latter will fail at runtime if the reference you're typecasting isn't actually of the type you're casting to or one of its descendents.

     // this won't compile because the compiler knows 
     // that a Transform isn't a Rigidbody or one of its descendents
     Rigidbody r = o.GetComponent<Rigidbody>();
     Transform t = (Transform)r;

     // this will compile because at compile time the compiler only 
     // knows that `r` holds a reference to a Component or a descendent,
     // but doesn't necessarily know what type the descendent is - 
     // however it will fail at runtime because when you try to cast r to
     // a Transform the runtime checks the type and knows
     // that it's not a reference to a Transform.
     Component r = o.GetComponent<Rigidbody>();
     Transform t = (Transform)r;


I just found this error while working and know curious about why I can typecast from one Component type to other, or to an Object type but cannot cast to a GameObject type while I can cast from a GameObject type to a various component types.

Every Unity object descends from Object at the root of the class tree. So you can typecast any Unity object to an Object. But you can't necessarily go the other direction for the same reason I showed above.

But I'll repeat - you can't cast from one Component type to another.

avatar image
0

Answer by Nick_Artemov · Oct 14, 2015 at 01:50 PM

Thank you, Dave for your wide answer! Now it's clear to me.

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Car made from Different GameObjects and Empty GameObjects breaks on Collision with Terrain. 0 Answers

How Do I Keep Rigid Bodies From Going Through Each Other? 3 Answers

Physics Engine doesn't work,Collisions never work 0 Answers

Accessing the rigidbody of a cloned Object? 1 Answer

How to restrict object's movement to a bezier curve? 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