- Home /
UI Buttons generated through script appearing UNDER other UI elements, how to change?
Hey everyone,
So if the title isn't descriptive enough, I am instantiating a button prefrab through a script to create a sort of "drop down" menu.
foreach (string army in Armies) {
GameObject newButton = (GameObject)Instantiate (buttonPrefab);
newButton.transform.SetParent (transform.parent);
lstButtons.Add (newButton);
RectTransform btransform = newButton.GetComponent<RectTransform> ();
btransform.position = new Vector3 (transform.position.x, transform.position.y - offset, 0);
offset += 30;
Text armyText = newButton.GetComponentInChildren<Text>();
armyText.text = army;
}
This is my code.
As you can see it creates the prefab and I have tried changing the .position z value and have gotten no change.
These newly created buttons are appearing under other buttons on my UI instead of over them so they can be clicked as part of a menu.
So my question is how do I change UI elements like buttons rendering order so that newly instantiated objects will appear ontop of the UI?
Thanks, Kriogenic.
Answer by Kiwasi · Feb 01, 2015 at 10:13 AM
Render order for UI elements is done by hierarchy order. The Transform.SetAsLastSibling method may help. There are other sibling methods available.
Thanks alot, I didn't think about hierarchy order as being an issue. I moved the parent object of the drop down to be the last thing on the canvas and problem solved.
Your answer
Follow this Question
Related Questions
Unity 4.3 - 2D - Velocity based on rotation 1 Answer
Unity 4.3 - 2D demo - question about jumpForce 3 Answers
Crash when import Facebook SDK 0 Answers
How Do I Animate a Sprite in 4.3? 1 Answer
Problem with lights in 4.3 1 Answer