Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Setting anchoredPosition doesn't work?

Discussion in 'UGUI & TextMesh Pro' started by pea, Aug 28, 2014.

  1. pea

    pea

    Joined:
    Oct 29, 2013
    Posts:
    98
    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:
    Code (CSharp):
    1. RectTransform myRectTransform = myGameObject.GetComponent<RectTransform>();
    2. Debug.Log(myRectTransform.anchoredPosition);
    3. myRectTransform.anchoredPosition.Set(100,100);
    4. Debug.Log(myRectTransform.anchoredPosition);
    The position of the object doesn't change and the log output is:
    Code (CSharp):
    1. (-373.0, -52.0)
    2. UnityEngine.Debug:Log(Object)
    3. (-373.0, -52.0)
    4. 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?
     
    Last edited: Aug 28, 2014
  2. pea

    pea

    Joined:
    Oct 29, 2013
    Posts:
    98
    Got it! This works:

    Code (csharp):
    1. myRectTransform.anchoredPosition = new Vector2(100,100);
    Not sure why the Set() method exists? Maybe it's not the setter method?
     
    choi19131, zaher_it and Lucky28 like this.
  3. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    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.
     
    pea and runevision like this.
  4. pea

    pea

    Joined:
    Oct 29, 2013
    Posts:
    98
    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.
     
    Senshi likes this.