spawn UI elements on top of other gameObjects

Is it possible to spawn UI elements on top of a gameObject in the scene and have it position itself according to the canvas? Instantiating UI elements at the transform of a gameObject does not work, since it will be outside of the canvas. I dont want to manually create several transforms on the canvas for where my UI will be spawned at. I just want to instantiate a gameobject and also instantiate some UI elements at the same position and have it be visible in the scene. Somebody here who is cunning in using UI in unity and knows the solution to this? Much appreciated

You can give the GameObject its own canvas child GameObject, and make the UI elements a child of that canvas. This is how I handle UI text for hit damage.

I also render all UI elements with a separate camera with a higher depth value than the main camera. That may or may not be needed depending on your situation. In my setup, all UI elements are put on a UI layer, and the main camera does not draw the UI layer. The second camera only draws the UI layer.



I suggest u to not use UI for this;

even if u manage to set the position u still may have problems like rotaition or else;

i managed to solve this buy an old system of unity named “GUITexture” like this:


the benefit of guiTexture is that it won’t ever rotate ;
( dont know if your game is 2d or 3d )

and it has some variable like “width” or “X” to control the lenght of it just like UI;

it dose not need canvas or any thing and u can set its position based on camera to object;