Pulling my hair out! Camera.ScreenToWorldPoint() acting strange [SOLVED]

Hello,

I’m making a game with a currency that is located in the top left of the screen. It will be on android and different devices have different screen ratios, so I decided I wanted to work with the position relative to screen space rather than world space on this particular UI element.

The object is on a canvas (obviously) and should be in the top left, 10 pixels down and 10 pixels to the right. To do this I’ve made this script:

 transform.position = Main.ScreenToWorldPoint(new Vector3(10, Main.pixelHeight - 10, 0));

this script works 100% correctly when you are looking in the scene view, but nothing shows up on the game view.

one fact, though, debunked most of my hypotheses; changing the transform.position to something like vector3.zero allowed the game view to be able to see it again. Another thing to note is that this didn’t work when I built it on my android game, either. You still couldn’t see the currency.

the canvas is based on world space and is a child to the camera. the UI element is child to the canvas, because when the camera moves around I still want the text to be in the same position.

Does anyone have any idea whats going on? thanks!

You’ve got a few issues going on.

You must be using World Space on the Canvas. The reason why you aren’t seeing anything in the Game View is because the Image is within the Camera’s near clipping plane.

Additionally, this is the result I get once fixing the first issue.

Why don’t you just put the Canvas in Screen Space - Overlay mode and set the currency’s anchor and pivot to be in the top left. Then, you can just set the position to be (10,-10, 0) and not have to worry about any of this?

Example.

The canvas was on world space. What I meant is I wanted the position of the text to be relative to the screen’s top-left corner instead of putting it onto a world point and it then being clipped out of view on some tablets

I couldn’t just put it to 1(0, -10, 0) because even though the camera would fit the screen’s size the text wouldn’t because the scale of the camera was changed, not the position. what I was doing was actually for good reason.

In any case, I figured out my problem. Basically the text’s Z coordinates were changed when the screen coordinates were translated into world coordinates. It ended up being something like -3000 for some reason, and because I’m making a 2d game I never even thought to check that haha