- Home /
Problem copying RectTransform UI elements
Hi all,
I'm having an issue where, more often than not, if I try to copy a child element of a UI gameObject (i.e. something with a RectTransform and CanvasRenderer), it gets created, but isn't visible. Instead, Unity displays a grey transparent disc where it should be.
In the Editor, copy-pasting the values from the previous object's RectTransform component into the new copied RectTransform fixes the issue - but no value appears to have changed.
Worse, in code, I can Instantiate() copies, but the same issue happens, and I know of no way to 'fix' the RectTransform in this case. This happens both with copying an existing element in the hierarchy, and with Instantiating it from a prefab.
Is it entering some kind of disabled / invalid / un-renderable state? If so, what's the cause, and why does refreshing the (apparently) same values on a RectTransform fix it?
Thanks!
After you instantiate them, do you change the parent? (Using transform.SetParent). If so, this method has also a second argument called worldpositionstays. Setting this to false can fix some of these issues.
Also, not calling SetParent at all after Instantiating might yield the same problems. Not 100% sure though.
If this is the case, then the problem is that the thing you try to copy is a child of another RectTransform. When you then instantiate it, it gets instantiated with a different parent (no parent). And thus the resulting world position is wrong. (world position and size get calculated out of the values in the RectTransfom's inspector and that of it's parents)
If you then set the parent and keep the world position and size the resulting world position and size will still be wrong.
If you ins$$anonymous$$d set the parent while not keeping the world position, the new worldposition and size that is calculated should be equal to the item you copied (Given they have the same parent)
I think I am seeing the same problem -- click anywhere in the hierarchy, create a new UI element like Image or Panel, Unity inserts the item in a see$$anonymous$$gly random place in the hierarchy. So naturally one wants to move it to the correct place i.e. inadvertently change transform parent, which breaks the Rect Transfom (the little anchors graphic where you set the anchors is just greyed-out) and the item no longer renders
If you can replicate the problem, you might perhaps want to report it as a bug if you think it is a bug. I think it might be something else, but given the limited information you have, all I can do is guess :p
Do you mean that the item's anchors and width/height/etc. are greyed out in the inspector? If so, it means something else is setting it. For instance a Layout group on its parent or a contentsizefitter on itself (because you say it happens when you change parent, it's most likely a layoutgroup on the new parent).
If that is the case, you can add a LayoutElement to your element and make sure the calculated width/height value is higher then 0 ;)
Another aption would be to add a LayoutGroup to your element for the same reason (In case you want child(s) of your element to control its preferred width/height).
And yet another option could be to change the properties of the parent LayoutGroup (checking the child force expand bools for instance)
Also, when rightclicking in the hierarchy, followed by UI-->[something] in the context menu ins$$anonymous$$d of UI-->[something] from the GameObject menu in the top. The new Image/Panel/etc. will be placed as a child of the object you right clicked (only if the object you clicked has a canvas somewhere up in his hierarchy).