c# - Move UI component after it was instantiated -


i creating 3d game going strategy game. want create panel (ui group of bunch of texts , buttons) code (c#) , move them on top of gameobject in canvas layout. doing this:

 if (thisbuilding == null)      thisbuilding = this.gameobject;  panel = canvas.instantiate(panel);  panel.transform.setparent(canvas.transform, false); //false prevent stupid scaling issues created parent change   vector3 centerposition = thisbuilding.getcomponentinchildren<meshrenderer>().bounds.center; //the building gameobject contains meshrenderer component in children.  panelrect.anchoredposition = vector3.lerp(panelrect.anchoredposition, centerposition, 1.0f); 

the thing script run (on start()) once per building, hoping panel instantiated 3 times , each panel corresponds building, strange reason not work.
expected result: each time panel gets instantiated, position same of building instantiated (the building gameobject holds script , activates / deactivates panel)
actual result: though panel instantiated 3 times should, in same position , not change when update() function changes position. doing wrong?

giving world position of buildings results in panels being positioned in same location on screen. needed calculate world position screen position , use calculated screen position place panels.

try this:

panel.transform.position = camera.main.worldtoscreenpoint(this.gameobject.transform.position); 

Comments