• 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 /
avatar image
0
Question by alvmlg · Apr 06, 2018 at 03:36 PM · camera2dpositionmousenull reference exception

Camera main is not callable

I have a multiplayer game and the Main Camera is disabled.

Each Player has it's own Camera component attached to it with a script called PlayerController.

Each Player has a child script component which needs to access its mouse position.

I access then the parent camera like this:

 GameObject playerCamera = transform.parent.GetComponent<PlayerController>().plCam;

But I am having problems trying to calculate its mouse position:

 Vector3 mouseWorld = playerCamera.GetComponent<Camera>().main(Input.mousePosition);

It says Camera.main is not callable.

The problem if I just use Input.mousePosition it's the position is not relative to the Player.

I have the player camera tagged as main and active and enabled, I dont know if I am missing something else.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Reynarz · Apr 06, 2018 at 03:44 PM

You don't neet to call .main in playerCamera.GetComponent(). main is not a method, is a property. for that reason you can't use the ( )

Comment
Add comment · Show 7 · 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 · Apr 06, 2018 at 04:09 PM 0
Share

Furthermore the .main property is a static property of the Camera class. So you can not access it through an instance of Camera but you have to use just Camera.main. However from the code it seems he wants to use ScreenToWorldPoint or ScreenPointToRay depending on what's the actual goal.

avatar image Reynarz Bunny83 · Apr 06, 2018 at 04:12 PM 0
Share

Thanks for the correction :)

avatar image alvmlg Bunny83 · Apr 06, 2018 at 04:32 PM 0
Share

Then can I just access ScreenToWorldPoint from playerCamera directly?

avatar image Bunny83 alvmlg · Apr 06, 2018 at 04:54 PM 0
Share

If your playerCamera is tagged "$$anonymous$$ainCamera" you can simply do

 var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);


If your player camera is not tagged $$anonymous$$ainCamera you should declare your variable just like this:

 public Camera playerCamera;

and either assign your playerCamera in the inspector or if you want to link it at runtime, assign the camera component from the gameobject it's attached to in Start. Once you have the Camera reference in your variable you can simply do:

 var pos = playerCamera.ScreenToWorldPoint(Input.mousePosition);
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

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

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

Related Questions

Mouse position not returning actual position 0 Answers

Rotate 3rd person Character according to mouse position 0 Answers

ScreenToWorldPoint for different resolutions in 2D 1 Answer

Top down movement and rotation 2 Answers

Mouse.position from center of player 1 Answer

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