• 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 /
This question was closed Oct 05, 2020 at 03:44 AM by Bunny83 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by djmorrsee · Jun 01, 2011 at 08:22 PM · c#cameramain

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?

Comment
Comments Locked · Show 1
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 nathanthesnooper · Oct 23, 2016 at 11:19 PM 0
Share

This has been answered, but in the future make sure your camera has the '$$anonymous$$ainCamera' Tag on it

4 Replies

  • Sort: 
avatar image
27
Best Answer

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?

Comment
Comments Locked · Show 9 · 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 djmorrsee · Jun 01, 2011 at 09:28 PM 0
Share

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!

avatar image Bunny83 · Jun 01, 2011 at 10:31 PM 0
Share

A very common problem :D

avatar image samsmithnz · Mar 25, 2013 at 06:50 PM 0
Share

If I could upvote you, I would. Thanks for the answer. I'd added a script file called camera.cs too.

avatar image Gurc · Jun 23, 2013 at 05:42 PM 0
Share

In 2013 I did same mistake. Lulz.

avatar image keineleben Gurc · Apr 03, 2017 at 07:15 AM 0
Share

in 2017 I did the same mistake as well. :(

avatar image geeeed Gurc · Apr 02, 2018 at 05:53 AM 0
Share

in 2018 I did same mistake... :)

avatar image RyanGar46 geeeed · Oct 05, 2020 at 02:04 AM 0
Share

In 2020 I have done the same mistake; thank you, kind soul.

avatar image Like · Oct 23, 2016 at 09:03 PM 0
Share

Five years and problem is still actual. Thanks for help!

Show more comments
avatar image
1

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!

Comment
Comments Locked · 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 Bunny83 · Jan 24, 2017 at 04:44 AM 0
Share

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.

avatar image TGD · Jan 26, 2017 at 12:48 PM 0
Share

wow couldn't believe that the only reason my similar script wasn't working is because my camera wasn't tagged maincamera

avatar image
0

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'.

Comment
Comments Locked · 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
0

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...

Comment
Comments Locked · 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 Bunny83 · May 31, 2019 at 11:31 PM 0
Share

Well, just reading the documentation is enough.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

No Camera Rendering when switching between cameras developing for Android 0 Answers

OnMouseDown and OnMouseUp not working,onMouseDown not working 0 Answers

Camera enable/disable problem. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges