Prefab appearing behind the canvas/background

Hia all,

I think I have the coding down quite decently now but man am I behind in working in the engine itself. For Tilevania I actually went ahead and bought some assets from the asset store. Wanted to make the game a bit “spiffy” as Rick says:).

Anyhow:

My problem is that when I actually try to use the prefab in Unity then it appears behind the canvas/background.

Since the prefab doesn’t contain sorting layers (or a Sprite Renderer) I can’t push it to a higher layer. I think I’m going quite crazy.

I just need to be able to make sure that I can get the prefab on the screen and not behind the background. When I have done that I will just push it in from a script, as I have already managed to make all of the animations work for it.

Examples of what I have tried from research:

1.
https://forum.unity.com/threads/prefab-instantiating-behind-background.270214/
–Did not work and even if it makes it a bit too “clunky”, rather not mess with the camera

2.

Problem I have here is that I can place the image in a sorting layer but I can’t place the prefab in one so it still appears behind.

First I thought it was a problem of how Unity compiled the program and where it draw the prefab last or something but since it’s messed up already in scene view I believe it to be something else.

Worst case scenario I will just follow the lectures to a tee, would just be fun to get this thing to work.

ANY HELP IS APPRECIATED:)

//Murten

PS.Please excuse the overblown size, it just makes it easier to illustrate.


Uh, is your background(Canvas) set to the UI layer? I believe that layers places things in front of others?

Hi, I went up to check.

Yes, canvas, background image and prefab is set to the UI layer.

So all of them got the UI layer but somehow my prefab gets sorted behind the canvas and the background.

I still assume that there is a problem with the sorting (which I can’t solve since I can’t sort the prefab) but I might just have no idea.

Everytime I run into problem with Unity it isn’t really the big stuff, to a large extent because I can lean on this course(!), it’s always the small stuff and it’s driving me absolutely mad

PS. Attaching image that shows that it’s the same thing with the prefab as child tot the Image.

Trying Sorting it into a Sorting Order, having the knight at a higher order than the canvas?

Morning,

As earlier mentioned the knight doesn’t carry a sorting order option, it does not have a sprite renderer. So yes, I thought that might solve the problem but since I can’t push a sorting order to it it won’t be possible.

Also it should be working just by childing it to the background parent, without sorting, so I think I’m doing something fundamentally wrong

Best

Murten

Hi Murten,

If the Render Mode of the Canvas component is set to “Screen Space - Overlay”, the content of the Canvas will always get rendered on top of the scene. By scene, I mean “things that get rendered by the camera”.

Hi Nina,

Then I’m correct thinking that but still I can’t get it to work. As you can see in the image I have it as overlay, as I usually have it but even though I have childed the knight to the image (background) and the canvas, it’s behind.

I have added a picture of the prefab as well. My question would be, if this comes down to a sorting order problem, can I add something to the prefab in order to be able to put it in a sort order?

I don’t want to take up too much of your time so I can just swap to the included assets:) Just gonna bash my head into the problem for an hour more!


Where is your Sprite Renderer? Can you add a Sprite Renderer component to your Knight in the Inspector panel?

Your custom game object may not include one, so a Sprite Renderer may have to be added to access Sorting Order properties, as that is where the Sorting Order properties live.

Since you mentioned a Prefab, you’d want to open the Prefab for editing first, then add a Sprite Renderer component.

Truth

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms