# Working with localPosition and Anchor Points programmatically

**URL:** https://discussions.unity.com/t/working-with-localposition-and-anchor-points-programmatically/652341
**Category:** Getting Started
**Created:** [January 17, 2017, 7:19pm UTC](https://discussions.unity.com/t/working-with-localposition-and-anchor-points-programmatically/652341 "2017-01-17T19:19:23Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![viveleroi](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/viveleroi/32/910275_2.png) [@viveleroi](https://discussions.unity.com/u/viveleroi)
#### Post date: [January 17, 2017, 7:19pm UTC](https://discussions.unity.com/t/working-with-localposition-and-anchor-points-programmatically/652341/1 "2017-01-17T19:19:23Z")

</div>

I’m rendering a dynamic UI text list, so I can’t set anchor points/position through the editor.

I believe I have the correct code to achieve the same, but the result isn’t working as I expect.

I instantiate a Text prefab, assign the text, and set the anchor points:

```auto
rect.anchorMin = new Vector2(1, 1);
rect.anchorMax = new Vector2(1, 1);

```

This should anchor the text object to the upper right corner of the parent. These values are treated as “upper right” when done through editor.

To test, I set the localPosition to 0, 0, 0 - however the text appears in the center of my panel.

I must be misunderstanding one of these properties or missing another.

---

<div class="post-metadata">

### Author: ![JoeStrout](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/joestrout/32/200738_2.png) [@JoeStrout](https://discussions.unity.com/u/JoeStrout)
#### Post date: [January 17, 2017, 10:10pm UTC](https://discussions.unity.com/t/working-with-localposition-and-anchor-points-programmatically/652341/2 "2017-01-17T22:10:41Z")

</div>

Don’t set localPosition if you want to work RectTransform-style. Instead, set [anchoredPosition](https://docs.unity3d.com/ScriptReference/RectTransform-anchoredPosition.html).

---

<div class="post-metadata">

### Author: ![viveleroi](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/viveleroi/32/910275_2.png) [@viveleroi](https://discussions.unity.com/u/viveleroi)
#### Post date: [January 17, 2017, 10:21pm UTC](https://discussions.unity.com/t/working-with-localposition-and-anchor-points-programmatically/652341/3 "2017-01-17T22:21:56Z")

</div>

Can you clarify why that is? The anchoredPosition says it simply moves the pivot point of the rect, which doesn’t sound like it involves positioning the element.

---

<div class="post-metadata">

### Author: ![JoeStrout](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/joestrout/32/200738_2.png) [@JoeStrout](https://discussions.unity.com/u/JoeStrout)
#### Post date: [January 17, 2017, 10:26pm UTC](https://discussions.unity.com/t/working-with-localposition-and-anchor-points-programmatically/652341/4 "2017-01-17T22:26:33Z")

</div>

The docs aren’t super clear, but why not try it yourself and see?

---

<div class="post-metadata">

### Author: ![viveleroi](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/viveleroi/32/910275_2.png) [@viveleroi](https://discussions.unity.com/u/viveleroi)
#### Post date: [January 17, 2017, 10:39pm UTC](https://discussions.unity.com/t/working-with-localposition-and-anchor-points-programmatically/652341/5 "2017-01-17T22:39:31Z")

</div>

I did and it works, but that seems contradictory to the functionality documented.

Nevermind, I guess I was interpreting the docs differently. It’s not changing the pivot location, but it’s changing the location _based_ on the pivot.

---

<div class="post-metadata">

### Author: ![Kiwasi](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kiwasi/32/351502_2.png) [@Kiwasi](https://discussions.unity.com/u/Kiwasi)
#### Post date: [January 19, 2017, 9:54am UTC](https://discussions.unity.com/t/working-with-localposition-and-anchor-points-programmatically/652341/6 "2017-01-19T09:54:48Z")

</div>

Screw anchors altogether. They suck. Especially from code. 😛

I like to use the layout elements instead. Explicitly define empty RectTransforms where you need gaps.

If you do need anchors, set them up on a prefab, rather then messing with them in code.

---

<div class="post-metadata">

### Author: ![JoeStrout](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/joestrout/32/200738_2.png) [@JoeStrout](https://discussions.unity.com/u/JoeStrout)
#### Post date: [January 19, 2017, 2:04pm UTC](https://discussions.unity.com/t/working-with-localposition-and-anchor-points-programmatically/652341/7 "2017-01-19T14:04:31Z")

</div>

I’ll take this rare opportunity to disagree with @Kiwasi on this one. The anchor points and anchoredPosition make pretty good sense to me, and I’ve successfully used them both in the editor and in code.

If you put the anchor min and max in the lower-left corner, then you can treat anchoredPosition as simply the old-fashioned “X and Y position of this object on the screen” (relative to its container, which is convenient).

But when you need to do something fancier, like make an object right-aligned or centered or whatever even as the container size changes, then the anchors make this pretty easy too.

Sure, it’s confusing at first… but believe me, it could be [much worse](https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithConstraintsinInterfaceBuidler.html). 🙂
