Reputation: 97
I'm trying to get the x position of the main camera in visual studio but whenever I try Camera.main it throws the error " 'Camera' does not contain a definition for 'main'" any idea why this is? It recognizes the Camera class, but doesn't give any main method.
Upvotes: 1
Views: 783
Reputation: 51
To Access the Camera you need to get the GameObject Camera. That means you need to make it as Menyus wrote with a tag or try to find it.
Here some options for you:
//Simple
[SerializeField] private Camera camera;
// Best Way to "Find" via Code but you need to set a Tag in the Editor
private Camera camera;
void Start()
{
camera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
}
//or also a possible way but you could get not the wanted Camera if you use more than one.
private Camera camera;
void Start()
{
camera = GameObject.FindObjectOfType<Camera>();
}
Upvotes: 1