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.
var rectangle:RectTransform;
...
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?
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”.
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.