When I try to modify the UI element position with anchoredPosition, I get an error
“Property ‘anchoredPosition’ is private and only accessible within class ‘RectTransform’“
This is the code:
this._startAR!.getComponent(RectTransform)!.anchoredPosition = new Vector2(0,0);
Hi Tomislav, the error occurs because the RectTransform class in Needle Engine has its anchoredPosition setter marked as private (as seen in its TypeScript definition at RectTransform.d.ts.md). In this design, you can read an element’s anchoredPosition, but you cannot assign to it directly from your code.
In Needle Engine the anchoredPosition is calculated and controlled internally as part of the UI layout system. Rather than setting it manually, you should update the public properties that govern layout—such as anchorMin, anchorMax, pivot, or even the transform’s position if appropriate. This ensures that the UI element’s layout remains consistent with the overall UI system.
If you really need to change the anchored position programmatically, one approach is to extend or wrap the RectTransform with a custom method that internally makes the change (but keep in mind that bypassing the intended design might lead to unexpected behavior).
I hope this helps clear up the issue. Let me know if you need further assistance!
4 replies left
thread_YeiZFlOSXPr229zqPSY4bsKk
Reply to this post directly if you need further help (bottom right button)