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??
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?
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.A Rigidbody is not a GameObject so you cannot cast to it.
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.What are you trying to do exactly?
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.
Answer by Nick_Artemov · Oct 14, 2015 at 01:50 PM
Thank you, Dave for your wide answer! Now it's clear to me.
Your answer
Follow this Question
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