- Home /
accessing camera from anther gameobject, not working
Hi, I tried the following code on update(),
convertedVector=GameObject.FindGameObjectWithTag("camera").GetComponent<Camera>().ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,camera.nearClipPlane+10.0f));
But unity is giving me a error like this,
MissingComponentException: There is no 'Camera' attached to the "whatTheFuck" game object, but a script is trying to access it.
You probably need to add a Camera to the game object "whatTheFuck". Or your script needs to check if the component is attached before using it.
gameobjtest.Update () (at Assets/scripts/gameobjtest.cs:15)
camera is attached to a gameobject tagged "camera" and the refering script is attached to a gameobject called "whatTheFuck". Now I am not trying to access any camera attached to "whatTheFuck" gameobject, rather I am trying to access camera from another gameobject. But unity is saying me electric. What is causing the problem?
no it is untagged. Only the gameobject named "camera" has the tag "camera". No other gameobject has this tag. And "camera" gameobject has a "Camera" component where there is a orthographic skybox camera attached to it.
you said "camera is attached to a gameobject tagged "camera" and the refering script is attached to a gameobject called "whatTheFuck"" ...
Do you mean to say the camera is its own GameObject and tagged "camera" or there is a gameobject with tag "camera" and a camera is its child? If so, I would do away with the parent object and just tag the Camera object as camera
camera is its own GameObject and the gameobject is tagged as "camera".
Oh I got it. The main problem was "camera.nearClipPlane" part.
Answer by kaiyum · Feb 18, 2014 at 10:44 AM
The main reason of why my code did not work is because of:
camera.nearClipPlane+10.0f
Here nearClipPlane is referring to clip plane of this gameobject's camera component. But there is no camera attached to this gameobject. Camera is attached to a gameobject with camera tag. So we have to refer that gameobject. Thus in stead of using:
camera.nearClipPlane+10.0f
We have to write like,
GameObject.FindGameObjectWithTag("camera").GetComponent<Camera>().nearClipPlane+10.0f;
This will work just fine.
Answer by JP3ACE · Sep 28, 2016 at 08:43 PM
I know I am late, but I think I just figured it out.
Make sure the Game Object you want to see through has the "Player" Tag
Also,
Make sure the camera attached to that Game Object has the "Main Camera" Tag
It just worked for me, no need to write extra code, no errors. Hope this helps!