- Home /
Instantiating into UI objects
Hi I have this prefab of a button that I instantiate as a child of a panel in my canvas. But unfortunately it instantiates it with the anchor points in the right place, but the properties values of the rect transform are really out of balance. ( now they are something like left : - 24 , Top : 380 , right : 260 , bottom : -334 ) And they should be all zero. See if I manually change these numbers to zero while in play mode the button appears as I would like them to be.
So that leads me the my first question : Can I set the rect transforms properties all to 0 while instantiating them ?
I also have a 2nd question : After I can successfully instantiate buttons in the right position, can I then make it so that each time I add a button its below the other buttons ? lets say that there is like 10 pixels between every instantiated button ?
Thank you for reading this.
Answer by KayelGee · Jun 17, 2015 at 01:01 PM
Yes you can. You have to set the positions to all zeros after you have set the parent.
The answer to your second question is also yes. You just need to keep track how many buttons you have added via a variable that counts up and multiply that with whatever distance you want your next button be away. Alternatively you could add a Vertical Layout Group which should align your GUI elements automatically.
Your answer
Follow this Question
Related Questions
Re-sizing UI text font relative to screen size 3 Answers
Strange UI scrollbar handle recttransform behaviour,Cant resize clickable area on UI handle 1 Answer
[Unity 4.6 Beta] Anchor snap to button (New UI System) 3 Answers
Instantiate object on mouse click and when cursor IS NOT on UI 1 Answer
UI prefab instantiation problem 1 Answer