Camera.main not working in C#

Ok, im having quite the issue here, and I’ve searched and couldn’t find any similar posts. Im following this guide here about object labels for a level selection type thing. I’ve got this Java code here, and it works perfectly:

var target : Transform;
var offset = Vector3.zero;
private var cam : Camera;
private var thisTransform : Transform;

function Start () {
	target = GameObject.Find("CubeTest").transform;
	thisTransform = transform;
	cam = Camera.main;
	guiText.text = "test";
	guiText.material.color = Color(0,1,1);
}

function Update () {
		thisTransform.position = cam.WorldToViewportPoint(target.position + -1*offset);
}

But im trying to keep my whole project in C# so this is what I have:

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
private Transform target;
public Vector3 offset = Vector3.up;
public Camera mainCamera;
private Transform selfTransform;

	// Use this for initialization
	void Start () {
		target = GameObject.Find("CubeTest").transform;
		selfTransform = transform;
		mainCamera = Camera.main;
		guiText.text = "test";
		guiText.material.color = Color.green;
	}
	
	// Update is called once per frame
	void Update () {
		selfTransform.position = mainCamera.WorldToViewportPoint(target.position + -1*offset);
	}
}

For some reason with the C# script, im getting the error “‘Camera’ does not have a definition for ‘main’” which isnt making sense to me, seeing as the Javascript worked just fine.

Can anyone shed any light on this?

That should compile correctly as far as I can see. Are you sure your project doesn’t have a script named Camera or a class named Camera somewhere which could screw things up?

What just happened to me: I had a script that changed the layer and tag of the main camera. Also a bad idea!

i think it’s also a good idea to keep the camera on the top level in the hierarchy view. it happened to me, that i’ve had the camera in a sub folder. so c# couldn’t detect the camera in ‘main’.

I did Camera.main.enable = false, and after that Camera.main is null… so had to save Camera.main in a variable or I would never be able to enable it again… Maybe it’s intended but still unexpected…