Malfunctioning code

I was writing a script to move a 3d object to the bottom right corner of the screen. Here is the code:
screenPosition = new Vector3(Screen.width,0,2); Camera cam = Camera.current; Vector3 point = cam.ScreenToWorldPoint (screenPosition); transform.position = point;
The code works fine on level 1, but not level 2. On level 2, it puts the object in a weird place. also, on all levels, it throws a null reference exception. Is this a glitch in unity or is the code’s fault? (note, a previous version of the code, which did the gave the wrong errors goos as so:
screenPosition = new Vector3(Screen.width,0,2); transform.position = Camera.current.ScreenToWorldPoint (screenPosition);

“Camera.current” can only be used in certain callbacks which are usually related to rendering, You can not use it inside Update, LateUpdate, FixedUpdate or similar callbacks. Camera.current is dynamically set right before a rendering callback is invoked. So depending on the context it could be null or any of the active cameras.

You usually use Camera.main since in most cases you only have a single camera and that should be tagged as maincamera. If you have multiple cameras you have to manually reference the right camera. You can define a public variable of type Camera and simply assign the desired camera in the inspector.