- Home /
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.
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 ( )
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.
Then can I just access ScreenToWorldPoint from playerCamera directly?
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);
Your answer
Follow this Question
Related Questions
Mouse position not returning actual position 0 Answers
Top-Down 2D game:How to make the camera move on hitting the sides of the screen? 2 Answers
ScreenToWorldPoint for different resolutions in 2D 1 Answer
Raycast from Character to Mouse Position? 2 Answers
Rotate 3rd person Character according to mouse position 0 Answers