- Home /
Typecasting Camera to Object for ObjectField
I'm probably totally doing this wrong totally wrong due to lack of understanding but how do a cast a Camera to an Object so I can use it in an ObjectField? I've tried the following but I get the error Error "Cannot implicitly convert type 'UnityEngine.Object' to 'UnityEngine.Camera'. An explicit conversion exists (are you missing a cast?)".
_target.cam = EditorGUILayout.ObjectField("Camera", (UnityEngine.Object)_target.cam, typeof(Camera));
Answer by Bunny83 · Jul 09, 2011 at 01:29 PM
You've casted at the wrong place ;)
A down cast is always implicit. A Camera is also an Object but not all Objects are Cameras.
ObjectField needs any Object as parameter and you can pass whatever is derived from Object, but it also returns just an Object. And nobody can say if that's a Camera or not, so that's the point where you need to cast the Object into a Camera (like the error said ;)).
_target.cam = (Camera)EditorGUILayout.ObjectField("Camera", _target.cam, typeof(Camera));
you just posted as I hit send ;). Could you read my comment and tell me if my understanding of the controls is right?
Answer by Blankzz · Jul 09, 2011 at 01:30 PM
Ok so I sat back and had a think as to what I was doing and obviously you have to cast the the ObjectField to the type of your object like so.....
_target.cam = (Camera)EditorGUILayout.ObjectField("Camera", _target.cam, typeof(Camera));
My understanding of how it all works is limited but I'm assuming the UI controls inherit form the type of object you are trying to display in it otherwise you wouldn't be able to type-cast it. Could somebody clarify this for me? If this is the case why couldn't there be a controls that inherit from different collection types so creating UIs for collections would be a lot easier? Also if I'm right the documentation should state what the UI control inherits from even though its should be obvious!
Unity uses an immediate GUI system. You have to call the function each frame to draw that control and handle the input. There are no UI controls in the scripting environment. ObjectField is just a function that displays and handles a UI object field in the editor. The object you are providing is the one that is displayed in the object field. If you change the object be dragging something on the field it get's returned by this function.
The ObjectField works with all objects that are derived from UnityEngine.Object. The type parameter is just there to restrict the field from accepting drag&drop operations of objects that are not of this type.
By using typeof(Camera) you make sure that only Camera instances can be assigned to the field and you can savely cast the result to Camera.
You can use the ObjectField as pure displaying field by ignoring the returned value.
Answer by cedw032 · Sep 05, 2014 at 12:38 PM
A camera has an object. The object really has the camera, as it is a component. but to get the GameObject for your camera, you should just be able to type gameObject and have access to that from where you are. If you are looking for the thing that actually is the camera, typing camera from that script will give you that. Helps?
Your answer
Follow this Question
Related Questions
Make compiler treat an object variable as if it was of type X 2 Answers
How can I change camera when colliding with "X" object? 1 Answer
How to set the position of objects on the left and right of the screen 0 Answers
focus on a position 1 Answer
Object parented to MainCamera and Y Axis Question: 0 Answers