- Home /
Unity 2D 4.6b UI: Why are instantiated prefab sprites rendering over UI elements?
I'm working on a 2D game with Unity 4.6 beta, and I'm using the new UI elements to make some buttons on a GUI overlay on screen. Whenever I instantiate a prefab (with a sprite) from code, however, the sprite is rendered on top of the UI elements. Why is that, and how can I change this behavior?
I've tried setting the layer of the prefabs and also through code on run-time to set it to a user defined layer, but it didn't work. Am I doing something wrong?
Answer by troien · Nov 16, 2014 at 10:49 AM
I havent tested this yet, but I think you should play with the 'Sorting Layer' property (or the 'Sort Order'/'Order in Layer') of both the SpriteRenderer and the Canvas Components.
(See the link(s) above for how to use these Layer(s))
@troien Yes! I'd been tinkering with the sorting layers, but I thought it wasn't the answer since the buttons and UI elements didn't have a Sprite$$anonymous$$anager component where I could set the sorting layer. Since you mentioned it, I went back to look for it again, and found it in the Canvas component of the Canvas object created for the UI. Now it renders correctly. Thanks!