damon
damon

Reputation: 2897

Why is Unity Canvas Image always rendering over my gameObjects even though it is definitely behind them?

Canvas image always over gameObjects

In my 2d Unity project, I have a Canvas with an Image that I want for a Background.

I have 2 gameObjects in front of this background. But no matter how much fiddling I do with Pos Z, Sorting Layers, or hierarchy sorting, the image is always in front of the objects.

Gif above shows in 3d mode that even though the image is clearly behind these objects, it will always appear over them if they overlap.

Hierarchy:

Any advice to rescue me from this madness is much appreciated.

Upvotes: 3

Views: 15165

Answers (8)

cass
cass

Reputation: 1

In the end I solved it by changing the game objects into images instead - go to canvas and create new UI - image - and then attached the sprite that way... it worked for my context, but may not work with your set up :)

Upvotes: 0

harddrive225
harddrive225

Reputation: 21

I figured out a workaround. I basically created a VisualElement inside the UI Builder and set a render texture to the background. Then I added an extra camera to my project to view all the sprites that needed to be on top. That camera feeds the render texture, so now everything that camera sees is forced to be on top of the UI Document as the background of that VisualElement. If you want control over the whole screen, just set the VisualElement position to absolute and max out its dimensions. If your game doesn't have a fixed aspect ratio it might cause some stretching, but other than that I cant really tell the difference. Sorting layers for the UI Documents are broken and unity needs to work on that. This is the best option I've found. Hope this helps.

Upvotes: 2

user19414691
user19414691

Reputation: 16

Had this same issue and was able to fix it with these steps:

  1. In canvas settings change Screen Space Overlay to Screen Space Camera
  2. Set the camera variable to the one you are using for your scene.

Upvotes: 0

Adi Shumely
Adi Shumely

Reputation: 397

  1. Inspector tab of the object or background.
  2. Sprite Renderer.
  3. Additional Settings.
  4. Sorting Layer.
  5. change it to a different layer.

Upvotes: 0

SamAdda
SamAdda

Reputation: 1

You could create a layer called Object Assign it to the game objects. Create an object camera culling mask -> object layer depth bigger than you current main camera. Set it to Projection -> Orthographic Clear flags -> solid colors. canvas Render Mode -> Screen Space - Camera and assign the Render Camera to be the Object Camera

Upvotes: 0

1fkl
1fkl

Reputation: 1

Try clicking on Layers -> edit layers, inside sorting layers you can change the order grabbing layer, everything upper appears behind in the camera.

Upvotes: 0

Youssef Moalla
Youssef Moalla

Reputation: 1

I had the same problem and I fixed it by attaching the camera to the canvas which is screen space and finally changing the sorting layer of my object to -1.

Upvotes: -1

Hiren Katira
Hiren Katira

Reputation: 78

Set the sprite renderer's transform z value to 0 instead of 100

If that does not solve, please specify camera properties also, so I can try to recreate the exact setup.

Upvotes: 0

Related Questions