0
votes

I'm having real issues doing something I think should be quite easy so I am probably doing something very wrong! I have a sprite on top of a space background image which is sitting within a canvas which the camera is fixed over and can be moved around. This is the main scene.

I am at the point where I want the player to be able to add a room to the station. This basically consists of another sprite which can slot into a space on top of the station sprite. I can add the sprite to the image and see it on top of the station in the sprite view but I cannot see it in the game view while the game is running. I have tried:

  • Having a canvas which a gameoject can be Instantiated on and then setting the image to the new room image.
  • Adding the sprite to the base station sprite, making sure they are on the same layer and then changing the sorting layer and order in layer and I don't think I have done this properly.

The room sprite is a child of the station and I just cannot get it to show in game. Can anyone offer any suggestions as to things I may have missed?

Rooms are empty but need a room sprite to be added by player

Sprite added and can be seen in game view Nothing seen during runtime

1
Not seeing anything obvious here: I shoved a sprite as a child of another sprite and the child renders on top without my having to adjust anything. As long as the "order in layer" is the same value (or larger towards positive infinity) it renders on top (if different layers, then its based on layers). That said: sprites are not canvas images. Sprites will always render behind a screen space and camera space canvases.Draco18s no longer trusts SE
@Nick Proud You shouldn't really do this in UI, have you tried simply using sprites in world space? If yes why didn't that work out for you?Fiffe
It's probably a lack of understanding on my part for canvases. I was containing the sprites in the canvas as the camera is fixed within it as the space it can be moved around in. The main canvas also contains my HUD. Essentially I need the camera to start in a particular position overlooking the station. The player can then move the camera around looking down on the world whilst keeping the HUD elements fixed in place. Hence why i used the canvas. Any alternative methods I should be trying?Nick Proud

1 Answers

1
votes

You should not be rendering your entire game in a UI Canvas; they're designed for UI. In order to manage rendering orders within a Canvas object, you'll have to reorder your heirarchy (which is both slow and unnecessary).

From Unity's Canvas docs:

UI elements in the Canvas are drawn in the same order they appear in the Hierarchy. The first child is drawn first, the second child next, and so on. If two UI elements overlap, the later one will appear on top of the earlier one.

To change which element appear on top of other elements, simply reorder the elements in the Hierarchy by dragging them. The order can also be controlled from scripting by using these methods on the Transform component: SetAsFirstSibling, SetAsLastSibling, and SetSiblingIndex.

Instead, I'd recommend going through Unity's Rogue-Like Tutorial and, if you don't feel like going through the entire tutorial, at least take a peek at the Sorting Layers portion for an understand of how the game should properly utilize SpriteRenderer. They'll be far easier to manage.