UI stays behind game objects
Hi!
Currently I have a problem with my UI. Due to some warping that is being done with the camera (to fit the rendering nicely onto a projector while remaining 16:10) I have to use the "Screen Space - Camera" option on my UI. This has however caused the UI to stay behind my game objects. This can be seen on this image for example: The settings on the UI are as follows:
The main camera is set to the correct camera
The plane distance is set to 0.31, the cameras near clipping plane is set to 0.3
Sorting layer is set to 'Default'
Order in layer is set to '0'
Rect Transform scale is set to 0.0002443...
I've tried many different things and the only thing that worked so far was changing the rendering mode from 'Forward' to 'Deferred', but this caused a whole lot of game objects to be rendered bad.
Does anyone know how I might be able to solve this issue?
Answer by troien · Sep 12, 2016 at 09:24 AM
If you really have to use screen space camera instead of screen space overlay and your camera is that close to objects that the UI will clip through. I think you don't have that many options...
These are the ones I can think of:
Set the plane distance as low as possible (Think you already did that)
Make sure the UI component don't have their z position set (to anything above 0, as this will move them further away from the camera, higher chance they will clip through the objects you are looking at)
ignore point 1 and 2 and use another camera used only for the UI You can make this new camera a child of your existing main camera for instance. Make sure its culling mask is set to UI, your other cameras ignore UI and then play with the depth of your new camera (Make sure its value is higher then the depth of your main camera) And ofcourse make sure you set this camera as render target of your canvas. Also, set the new camera clear flags to depth only :p
I believe setting the sorting layer and order in layer has no effect as these values are ignored by 3D objects. If you are not using 3D gameobjects but 2D ones instead (Sprite Renderer instead of Mesh Renderer), they should work I believe ;)
The problem also occurs with objects that are far away from the camera, the distance doesn't seem to make any difference at all. Point 1 and 2 don't solve the issue and point 3 will probably solve it, but this causes some other issues and I would therefore like to find a solution where adding another camera isn't necessary.
"The problem also occurs with objects that are far away from the camera"
What do you mean by this? Perhaps an image of your scene view can clarify this :p
But that object that is behind the UI and gets rendered in front,
Does it use a shader that could cause it to be rendered in front? :p
Does it get rendered by the same camera as the UI or by a different one (with a higher depth value)?
"Does it get rendered by the same camera as the UI or by a different one (with a higher depth value)?"
That was it! There was a different camera rendering objects in front of the UI. I've now set that depth lower and the UI is on top as it should :) . Thank you very much.
Answer by rajan4uto · Sep 12, 2016 at 09:30 AM
if you are using "Screen Space - Camera" in canvas then decrease the value of "Plane Distance" in canvas.
Answer by Dream_in_code · Sep 12, 2016 at 08:33 AM
Did you trt ordering the layer of the gameobject? Try setting the value to negative.
Changing the 'Order in Layer' did not change anything, not even when I set it to extreme values.
Answer by PraetorBlue · May 02, 2020 at 05:47 PM
Adding a new sorting layer and adding my Canvas to it did the trick for me.
Answer by aligonabadi · Sep 24, 2020 at 12:22 PM
I had same Problem. Increasing Depth in camera settings, fixed my issue.
Your answer
![](https://koobas.hobune.stream/wayback/20220612092639im_/https://answers.unity.com/themes/thub/images/avi.jpg)