void Start () {
GameObject tempObject;
for (int i =0;i<maxCategoryNum;i++){
tempObject = Instantiate(CategoryButton) as GameObject;
tempObject.GetComponent<RectTransform>().SetParent(this.GetComponent<RectTransform>(), false);
Vector3 temppos = tempObject.GetComponent<RectTransform>().position;
temppos.x=0;
temppos.y=i*tempObject.GetComponent<RectTransform>().rect.height+10;
tempObject.GetComponent<RectTransform>().position = temppos;
tempObject.GetComponent<SetCurrentCategory>().ID=i;
tempObject.transform.FindChild("Text").gameObject.GetComponent<SetText>().setText(Globals.categories*);*
}*
}* What is going on is that the buttons for categories seems to spawn from the bottom of the panel instead from the top. And to top it off they spawn in reverse order. How to fix that?
That is probably because you are setting the position. Which is generally not what you want to do with the Unity UI.
Instead most of the time you want to use Layout groups on the parent (HorizontalLayoutGroup, VeritcalLayoutGroup or GridLayoutGroup). Because then you don’t have to worry about setting the position through code a all, just add them as a child and these Layout groups will handle the rest. (Might need a LayoutElement on the child with values other then 0 though, to make it work depending on the situation)
The reason why this happens is because RectTransform.position is the same as transform.position. And when you look in the scene view, you will see that position [0,0,0] is the bottom left corner of your canvas. [0,heihtOfCanvas,0] is the upper left corner. So you actually place them from bot to top a.t.m.
If you don’t want to use layout groups, I would suggest trying to set variables that belong to the RectTransfom, like anchoredPosition, sizeDelta, etc. etc. instead of position. Or just use position anyway keeping the previous paragraph in mind, but note that it will break if you change the Canvas RenderMode