• 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 /
avatar image
0
Question by Blankzz · Jul 09, 2011 at 12:59 PM · cameraobjectcasttypecastingcs0266

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

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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));
Comment
Add comment · Show 1 · 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 Blankzz · Jul 09, 2011 at 01:32 PM 0
Share

you just posted as I hit send ;). Could you read my comment and tell me if my understanding of the controls is right?

avatar image
0

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!

Comment
Add comment · Show 1 · 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 Bunny83 · Jul 09, 2011 at 02:46 PM 0
Share

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.

avatar image
0

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?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to set the position of objects on the left and right of the screen 0 Answers

pragma strict convert object error 2 Answers

How can I change camera when colliding with "X" object? 1 Answer

focus on a position 1 Answer

Object parented to MainCamera and Y Axis Question: 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