- Home /
The question is answered, right answer was accepted
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?
This has been answered, but in the future make sure your camera has the '$$anonymous$$ainCamera' Tag on it
Answer by TowerOfBricks · Jun 01, 2011 at 08:32 PM
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?
I definitely should give you a cookie for that one. As soon as you said that it clicked in my head. $$anonymous$$y coding partner had a script named Camera that was the problem. Thank you!
If I could upvote you, I would. Thanks for the answer. I'd added a script file called camera.cs too.
In 2020 I have done the same mistake; thank you, kind soul.
Answer by maewionn · Jan 23, 2017 at 02:55 PM
What just happened to me: I had a script that changed the layer and tag of the main camera. Also a bad idea!
Have you realised that this question is almost 6 years old?
Also your "answer" doesn't answer the question at all. The problem was that he named one of his scripts "Camera" that's why the compiler complained that Camera.main
doesn't exist. So while it's indeed not a good idea to re-tag the main camera, it has nothing to do with this question.
Your "answer" looks and feels a bit like a twitter post. UnityAnswers is not a forum but a Q&A site. Even when posted on the forums, necroposting is is a no-go unless your post actually improves / solves something in regard to the question.
wow couldn't believe that the only reason my similar script wasn't working is because my camera wasn't tagged maincamera
Answer by beekobeeko · May 24, 2017 at 07:49 AM
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'.
Answer by rh_galaxy · May 31, 2019 at 09:06 PM
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...
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Smooth Follow Camera Rotate on Z-Axis? 2 Answers
Regarding transform.position in the roll a ball tutorial 1 Answer