1
\$\begingroup\$

I have set up an enemy prefab that gets instantiated during Start. Each enemy object has a health bar GUI object attached to a Canvas which is a child of these spawned enemy objects. So, when I spawn 10 enemies, 10 health bars get spawned as well. I have another GUI object for the inventory which I can open and close at the press of a button. When I open my inventory and there are enemies behind it, their health bars show on top of my inventory. I would like to layer the GUI objects such that these enemy health bars do not appear above my inventory. How do I do this? I have tried ordering them differently in the hierarchy as per someone's suggestion in a forum post, but that didn't seem to work.

Again, every enemy object has its own Canvas of which the health bar is a child. The inventory GUI object is attached to a different Canvas which has no parent.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

If the health bars are in "World Space" mode, which they should be in, and the inventory canvas is in "Screen Space - Camera" mode then you need to set the distance of the canvas from the camera.

If the inventory canvas is in "Screen Space - Overlay" mode, there really shouldn't be a problem like this, since it will be overlaid on top of everything else.

If the inventory canvas is in "World Space" mode, then you just need to make it closer to the camera than the other World Space canvas elements.

See also: Canvas.renderMode and RenderModes you can set it to.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks, you've given me a good idea of where to look. Both my inventory and the enemy health bars were in Screen Space - Overlay mode, and the problem was occurring. \$\endgroup\$ Commented May 31, 2017 at 10:44
  • \$\begingroup\$ @embracethefuture This comment comes a little long after the fact, but might come in handy for another reader (or possibly you, learning a thing): You can also use child objects of your canvas to arrange things, e.g. you have a separate object in your hierarchy called "health bars" and it's arranged in the hierarchy above the "inventory" object (and thus behind). Comes up more when you have two sets of prefabs that exist in the same space, spawned in pairs, but need one set to be behind all of the other set. \$\endgroup\$ Commented Dec 9, 2017 at 2:10
0
\$\begingroup\$

If you want the canvases of both UI elements to be rendered in Screen Space - Overlay mode, you can use Sort order to layer them in the desired way. The higher the sort order number, the later it gets rendered, and thus, the "closer to the front" it is.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .