Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

'PosX' is not a member of 'UnityEngine.RectTransform'.

Discussion in 'UGUI & TextMesh Pro' started by Tiles, Sep 8, 2014.

  1. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    I try to misuse a canvas image to draw a rectangle at mouse position. But i have problems to access the position values of the Rect Transform box by code.

    Code (JavaScript):
    1. var rectangle:RectTransform;
    2. ...
    3. rectangle.PosX=Input.mousePosition.x;
    Manual says:

    So i try to access the values by PosX. But i get an error: 'PosX' is not a member of 'UnityEngine.RectTransform'.

    I went through all possible ways to write posx now. But without luck. How do i access the Pos X value?
     

    Attached Files:

    • posx.jpg
      posx.jpg
      File size:
      79 KB
      Views:
      1,500
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    There isn't any PosX property just like a Transform component doesn't have a PositionX property; look up RectTransform in the scripting reference. There is an anchorTransform property (Vector2) which is "The position of the pivot of this RectTransform relative to the anchor reference point".

    --Eric
     
    StartStart and huloo like this.
  3. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Unity 4.60b18 here. Is there a newer version available?

    You confuse me, the image clearly shows that there is a Pos X component in the Rect Transform component. So it should be accessible, shouldn't it? :)
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Nope. You responded too fast and I had edited my post in the meantime, so see above. ;)

    --Eric
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    The image clearly shows there is a Pos X component in the RectTransform inspector. That is a very important distinction.

    If you change the way a RectTransform is anchored to its parent, the names of those fields change. Sometimes it's Pos X. Sometimes it's Left. All of those properties change depending on how it's anchored. Obviously the class won't change.

    These numbers are all actually convenient accessors for a more complicated pair of properties, anchorMin and anchorMax. Those convenience functions don't all exist in the scripting language, but it's all fairly simple math.

    While you're learning, you can use the Animation system as a kind of cheat sheet to see what properties you should be using in script.
     
  6. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Thanks folks :)
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    You can switch to debug mode in the inspector if you want to see the "real" properties.

    --Eric
     
    V5Studio likes this.