NullReferenceException With ViewportToScreenPoint()

So, I’m trying to convert the position of a GUITexture to the pixel position but I keep getting a NullReference Exception whenever I try. Here is my code:

Vector3 barPos;


barPos = Camera.current.ViewportToScreenPoint(sensitivityBar.transform.position);

I get the NullReference Exception when I get to the ViewportToScreenPoint function. I have no idea why this is happening. Any help would be much appreciated. Thank you.

The most likely culprit is “Camera.current.” According to the reference ‘current’ is “for low-level render control only.” Assuming you are using the default camera and have not changed the tag, you can use Camera.main.ViewportToScreenPoint().

The way I ended up fixing this was by translating it by myself.

What you need to do is take (screenWidth * ViewportXPosition) and (screenHeight * ViewportYPosition)

This should help.