Setting anchoredPosition doesn't work?

Odd one. I’m trying to move an anchored object by setting its “Pos X” to a new value. Except, nothing happens.

I’m doing this:

RectTransform myRectTransform = myGameObject.GetComponent<RectTransform>();
Debug.Log(myRectTransform.anchoredPosition);
myRectTransform.anchoredPosition.Set(100,100);
Debug.Log(myRectTransform.anchoredPosition);

The position of the object doesn’t change and the log output is:

(-373.0, -52.0)
UnityEngine.Debug:Log(Object)
(-373.0, -52.0)
UnityEngine.Debug:Log(Object)

Maybe this is because I have a Content Size Fitter and Horizontal Layout Group on there? I’ve tried removing them but no dice… Maybe it’s a bug?

Got it! This works:

myRectTransform.anchoredPosition = new Vector2(100,100);

Not sure why the Set() method exists? Maybe it’s not the setter method?

3 Likes

Well you are setting the value on the returned anchored position. It just so happens that it’s a struct though so it gets copied. You are setting the value on some intermittent object and not on the real representation.

2 Likes

Thank you for the clarification, that makes sense.

It’s great how Unity peeps like yourself are all over this 4.6 beta forum like a rash. This developer is feeling the love.

1 Like