- Home /
Access main camera from different GameObject script
I am making a scene editing utility, I have a GameObject which holds many textures that you can choose from to apply to prefabs, and I want to be able to place a line of 10 textured prefabs at the top of the screen using ViewportToWorldPoint in UnityScript.
I've tried everything I can find on the forum but I can't reference the main active camera to find its coordinates, how do I reference it from the texture editing GameObject's script?
Answer by CodeMasterMike · Nov 13, 2012 at 12:48 PM
Look at these:
Camera.current.ViewportToWorldPoint(Vector3.Zero);
// or
Camera.main.ViewportToWorldPoint(Vector3.Zero);
You can get the ViewportToWorldPoint information from two different ways in the Camera class:
Either with Camera.current or the Camera.main .
Read the documentation for the differences between those two.
Good luck!
thanks! I can't get this to work in update function, it returns an error
var p : Vector3 = Camera.current.ViewportToWorldPoint (Vector3 (1,1, camera.nearClipPlane));
same thing with Camera.main, it tells me
$$anonymous$$issingComponentException: There is no 'Camera' attached to the "spawnpoint" game object, but a script is trying to access it. You probably need to add a Camera to the game object "spawnpoint". Or your script needs to check if the component is attached before using it. mazemaker2.Update () (at Assets/Scripts/mazemaker2.js:150)
it says I'm missing a component on the script, even though I am looking for a GameObject ? Camera is GameObject?
@ZoomDomain
Camera and camera are two different things. Camera (with capital C) can be accessed from any script. While camera (with small c) is only accessable through a script that is attached to the camera object.