- Home /
UI changing position on instantiation on iOS
Hello fellow unityers! I have a problem with the positions of instantiated UI objects that only occurs when the project is compiled - I am using the new and wonderful Unity 5. I have a menu with a panel, with some elements inside (images and text) that are all positioned where i'd like with correct anchors, I use the following script to copy this panel, each time moving it to the right;
public GameObject firstPannel;
public int pannels = 10;
void Start () {
firstPannel.GetComponent<PlanetBoxConstructor>().construct(0);
for(int i = 1; i<pannels; i++){
GameObject newPannel = Instantiate(firstPannel, firstPannel.transform.position, firstPannel.transform.rotation) as GameObject;
newPannel.transform.parent = transform;
RectTransform rt = newPannel.GetComponent<RectTransform>();
rt.sizeDelta = new Vector3(0,0);
Vector3 newPos = newPannel.transform.position;
newPos.x = firstPannel.transform.position.x*(i+1);
newPannel.transform.position = newPos;
newPannel.GetComponent<PlanetBoxConstructor>().construct(i);
}
This works a charm in unity, and with other resolutions and screen sizes tried, but once compiled and put onto my iPhone5s all elements inside the copied panels are put into strange places. But are not this way in the original panel. The result:
I have tried making a script that resets the sizeDelta of each rectTransform to 0, one that sets all the anchor position to that of the element on the original but with no luck, here are my anchor positions, this picture is during runtime after the elements have been instantiated:
If anyone knows what I could be doing wrong I would be much appreciated :)
Thank you!
Answer by Carrel · Mar 30, 2015 at 08:24 AM
Let me answer my own question, I've found how to fix it so i'm posting this for others with the same problem... I believe it could be a bug! The parent object of the original panel wasn't full screen size, which is what i wanted, the top anchors where about 20% down, and the bottom 20% up, left and right about 5% in, all this with the sizeDelta/position of the rectTransform at 0. When I changed the size of the rectTransform to take up the whole screen size, I no longer have this problem!
as it's only this way when compiled and not in unity I believe it must be a bug!
Your answer
Follow this Question
Related Questions
Confused about game resolution settings for iOS 0 Answers
[SOLUTION] set UI RectTransform Anchor Presets from code c# 1 Answer
LineRenderer not instantiating on iOS or Xcode, Works fine in editor and Unity Remote 0 Answers
Trying to set RectTransform anchorposition in script 2 Answers
Anchor doesn't affect objects inside of another object 0 Answers