Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
11
Question by e-bonneville · Apr 22, 2010 at 11:24 PM · camerafind

Finding Cameras

How would you find a camera? GameObject.Find does not work because that only finds GameObjects. Is there a similar function for cameras? If there isn't, is there another way to get a reference to a camera via script?

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

8 Replies

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

Answer by Peter G · Apr 22, 2010 at 11:32 PM

To find the main camera, you can use Camera.main. Or to find an array of all the cameras in your scene, use Camera.allCameras.

Honestly though, I would still use GameObject.Find() and add .GetComponent<Camera>() to the end.

 // C#
 Camera cam = GameObject.Find("myObject").GetComponent<Camera>();

That way you can still find the camera by using Find() which is easier than finding it in an array.

Comment
Add comment · Show 3 · 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 e-bonneville · Apr 22, 2010 at 11:36 PM 0
Share

Ah. Couldn't find where to do that anywhere. Thanks a lot.

avatar image DtBeloBrown · Apr 23, 2010 at 01:48 AM 0
Share

GameObject.Find() is slow! Even GameObject.FindWithTag() seems a little much here. Be my guest, but Camera.main is just as easy and much faster. If you have multiple cameras just look through the array till you find one whose gameObject's name identifies it as the one you want.

avatar image Linus · Apr 06, 2013 at 09:44 PM 0
Share

As Peter commented in the other answer. Its is not meant to run every frame. You find the camera on Start() only.

avatar image
7

Answer by DtBeloBrown · Apr 23, 2010 at 01:41 AM

You have many options. Here are some, roughly in order of execution speed:

1) Simply drag a reference of the desired camera to a variable of type Camera in your script.

2) use Camera.main if the camera you want is the only active one right now.

3) If you have multiple cameras named uniquely, check foreach (Camera c in Camera.allCameras) and if one, c, whose .gameObject.name == "DesiredCamera" then that is the camera you want.

I do not recommend These next two options unless you need them. They are considerably slower and should not be done every frame:

4) GameObject.FindWithTag("Main Camera").camera

5) GameObject.Find("Main Camera").camera is slowest, don't use it unless you really have to.

See official docs for more

Comment
Add comment · Show 2 · 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 Peter G · Apr 23, 2010 at 01:59 AM 3
Share

Your numbers 4 and 5 are not supposed to be run every frame. The idea is to cache the object at the Start(). So you are correct that they are performance heavy, but in this case it would not make a difference because you are only calling the look up on the first frame.

avatar image Qorgyle · Jan 23, 2015 at 04:36 AM 0
Share

Is there an appreciable difference in speed between 3 and 5 if I run them only on Start()?

avatar image
2

Answer by Molix · Apr 22, 2010 at 11:29 PM

Camera.allCameras is an array of all active cameras. However, depending on the usage, you may just want to have your camera find you---e.g. tell you about itself when it Starts()---or find it once when you Start(), or put a reference member to the GameObject/Transform that contains it.

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
avatar image
2

Answer by li3ro · Jul 11, 2015 at 12:26 PM

in Unity 5 I use this:

  1. Goto camera and set a tag to it ("myCamera") for example

  2. In your code use the following sysntax:

Camera myCamera = GameObject.FindWithTag("myCamera").GetComponent<>Camera>();

nJoy

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 joaoVictorCardoso · Jan 01, 2018 at 06:24 PM 0
Share

Very thanks this works fine

avatar image
1

Answer by Harinezumi · Mar 16, 2018 at 09:26 AM

The easiest solution I know:

 Camera[] allCameras = FindObjectsOfType<Camera>();

(Of course, this thread is too old for anyone to see this...)

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
  • 1
  • 2
  • ›

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.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

Finding objects on scene 1 Answer

Get main cam from player after player was spawned 1 Answer

How do I find my inactive objects and set them active again? 3 Answers

Camera.main look up 1 Answer


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