- Home /
How to assign a rigidbody in custom inspector?
Hello there, i want to create a field in a custom inspector where i can assign a rigidbody but i get an error in this code:
public override void OnInspectorGUI() {
base.OnInspectorGUI();
var myScript = target as Grabable;
if(myScript.grabType == Grabable.GRAB_TYPE.FORCE) {
myScript.connectedRigidBody = EditorGUILayout.ObjectField("Connected Rigidbody",
myScript.connectedRigidBody, typeof(Rigidbody), true);
}
}
Error: "Cannot convert type UnityEngine.Object to UnityEngine.Rigidbody - are you missing a cast?"
How can i actually assign a rigidbody there? Casting the Rigidbody into a UnityEngine.Object is redundant (says Unity),
Greets :)
Answer by Lysander · Jan 05, 2018 at 01:50 PM
Other way around- ObjectField returns a UnityEngine.Object, you need to cast that returned object to a Rigidbody. Just add "as Rigidbody;" to the end of that line, or (Rigidbody) to the beginning, like:
myScript.connectedRigidBody = (Rigidbody)EditorGUILayout.ObjectField("Connected Rigidbody", myScript.connectedRigidBody, typeof(Rigidbody), true);
Your answer
Follow this Question
Related Questions
How can i get SerializedProperty from UnityEvent which in List. Sorry for my Eng. 2 Answers
Questions Regarding Images in Custom Inspector/Editor 1 Answer
Make a custom inspector that shows a group of variables in form of list 2 Answers
How to Hide/Show List or Array in the inspector based on a variable? 0 Answers
Scrollbar in Inspector 1 Answer