Problems with screen resolution

Hello, guys! I’ve been going through some problems when it comes to screen resolution because it seems I simply can’t create the GameObject on the right position depending on what screen resolution I’m playing.

An example, “Free Aspect”:

And “Maximize on Play”:

And the problem persists in every resolution I tested after building the game. It seems the game only works properly when running in Free Aspect.

Consider using Camera.ViewportToWorldPoint if you want the GameObject to be created in a position relative to the Camera’s viewport rectangle despite the screen size.

I tried something like this because I used a similar technique with the limits of the game using Camera.ScreenToWorldPoint but it didn’t work:

transform.position = new Vector2(mainCamera.ViewportToWorldPoint(new Vector3(Screen.width/-68.59f, 0f, 0f)).x, mainCamera.ViewportToWorldPoint(new Vector3 (0f, Screen.height/-84.85f, 0f)).y);

Viewport space [for the Camera] should be within the range of 0 and 1 on both the X and Y axis. So Screen.width/-68.59 for example might be far outside of the range of the screen (if width is 600 pixels for example, 600/-68.59 is outside of the range of 0 and 1).

Same thing for Screen.height/-84.85f

You also shouldn’t need to call it twice for screen width and screen height.

Try this:

transform.position = mainCamera.ViewportToWorldPoint(new Vector3( 0.5f, 0.5f, someDistance));

You’ll have to supply the value of someDistance. I believe its in World coordinates.

This should always place the position of the transform in the center of the screen, despite the screen size.

You can change the x and y values to get a similar effect relative to the sides of the Viewport of the Camera.

When I run the game, it’s possible to see that the GameObject’s position is correct but it’s sprite vanishes. Do you know why?

It’s extremely difficult to say without seeing your code. There could be a myriad of reasons as to why your Sprite vanishes.

-The Sprite is rendered on the incorrect layer (unlikely if your scene was already setup properly)
-The Sprite is being hidden by other objects because its Z value is under another object on the same sorting layer (likely, especially if you are using the code I supplied as a test and didn’t modify someDistance to a value suitable for your needs)
-Something is disabling the Renderer of your GameObject with the Sprite attached to it.

But I’m merely guessing. I have no idea unless I see how you’ve implemented the positioning of your GameObject and if I know what other Images are on the same Sorting layer as your sprite.

I’m willing to bet that the Z value (someDistance) is not properly set.

Nice bet, someDistance was indeed not properly set, already solved it and the previous problem with the screen resolutions as well, thanks!