Legacy Documentation: Version 2017.2 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Camera.main

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public static var main: Camera;
public static Camera main;

Description

The first enabled camera tagged "MainCamera" (Read Only).

The primary Camera in the scene. Returns null if there is no such camera in the scene. This property uses FindGameObjectsWithTag internally and doesn't cache the result. It is advised to cache the return value of Camera.main if it is used multiple times per frame.

no example available in JavaScript
//Place this script on a GameObject to switch between the main Camera and your own second Camera on the press of the "L" key
//Place a second Camera in your scene and assign it as the "Camera Two" in the Inspector.

using UnityEngine;

public class Example : MonoBehaviour { //This is Main Camera in the scene Camera m_MainCamera; //This is the second Camera and is assigned in inspector public Camera m_CameraTwo;

void Start() { //This gets the Main Camera from the scene m_MainCamera = Camera.main; //This enables Main Camera m_MainCamera.enabled = true; //Use this to disable secondary Camera m_CameraTwo.enabled = false; }

void Update() { //Press the L Button to switch cameras if (Input.GetKeyDown(KeyCode.L)) { //Check that the Main Camera is enabled in the scene, then switch to the other Camera on a key press if (m_MainCamera.enabled) { //Enable the second Camera m_CameraTwo.enabled = true;

//The Main first Camera is disabled m_MainCamera.enabled = false; } //Otherwise, if the Main Camera is not enabled, switch back to the Main Camera on a key press else if (!m_MainCamera.enabled) { //Disable the second camera m_CameraTwo.enabled = false;

//Enable the Main Camera m_MainCamera.enabled = true; } } } }

Did you find this page useful? Please give it a rating: