- Home /
The question is answered, right answer was accepted
how to access camera from another object
My script is attached to a character controller. And i declared
public Camera camera;
in class. In update function i given
if(Input.GetMouseButtonDown(0)){
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray, out hit3, 400.0F)){
print(hit3.collider.gameObject.name);
}
}
But i am getting error
"UnassignedReferenceException:
The variable camera of 'characterScript' has not been assigned.
You probably need to assign the camera variable of the
characterScript script in the inspector."
Help me with a good solution. Thanks in Advance
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
//to
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
you changed you question as before you had cam = GameObject.Find ......
now drag the camera component into the public you defined. see if that works
i tried. its showing error: Assets/Scripts/characterScript.cs(52,39): error CS1061: Type UnityEngine.GameObject' does not contain a definition for
ScreenPointToRay' and no extension method ScreenPointToRay' of type
UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
Note that Camera.main is access to the camera named "$$anonymous$$ain Camera" so you can do something like:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Answer by OmarAlhaddad · Feb 14, 2013 at 03:23 PM
you can use:
Camera.mainCamera
But make sure your camera has the "MainCamera" Tag
Follow this Question
Related Questions
C# FP_Camera not following Character Controller 0 Answers
C# locking Main Camera's Rotations 1 Answer
No Glitch Please ... 1 Answer
capture image on phone and use it as background of the scene 0 Answers