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));

You’ve casted at the wrong place :wink:

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));

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!

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?