- Home /
4.6.1 GUI. Get Pos Y property from script?
I am doing some black magic that can hopefully make use of the Pos Y property on uGUI elements.
I'd be SO happy if I could just return the correct value for this, but whenever I try, it doesn't work.
I am using a WorldSpace canvas.
I have tried to Debug.Log() the following:
recTransform.rect.position.y
rectTransform.anchoredPosition.y
rectTransform.position.y
rectTransform.anchoredPosition3D.y
it always returns zero like in the following example, except for anchoredPosition.y, which returns 2.0 for some reason.... wtf... what it SHOULD return is -10 :/
I hope using VerticalLayout component hasn't LOCKED my values to zero without telling me or something....
But it seems you have a layout group on the parent object, not sure you can get anything out of that.
Answer by Kerihobo · Feb 22, 2015 at 10:52 AM
fafase, you got it.
So I suppose I should have guessed, but it wasn't entirely obvious to me at first, indeed it seems that LayoutGroup components override the properties of RectTransform in order to get that spacing, scale and position for the grid. This only makes sense.
So what is the problem? Well, if you want to access your RectTransform while the parent has a LayoutGroup component, forget it.
Solution?
I found out that enabling the LayoutGroup ONCE, and then disabling it immediately after, allowed my children to retain their newly acquired transforms that they received from parent's LayoutGroup component. So... if you want nice organized grid, but you want to control the Rect Transforms too sometimes, maybe for animation at certain events... thennnnn...
Enable LayoutGroup component on parent.
Disable LayoutGroup componenent on parent.
job done, you have nicely aligned child GUI elements now, and you can access their posY property or any of their others.
Note: I did however need to apply a LayoutElement component to each of the children to set limits for their min height/width. (To counter the devastating effects of parent's LayoutGroup component).
For Interest's sake...
"Why do you want to do this anyway???":
For special FX, really magical ones that change based on properties of child GUI elements.