How to get the actual Position of a Canvas Child Object

Hello Dear Programmers,

I am very new to Unity, trying to make my first footsteps and already fallen into problems from which Im suffering since since lunchtime… :wink:

My Project is in 2D, and here is the problem
So I Created A Canvas-Object with 3 Childmembers (Buttons). OnClick the buttons are disappearing to the left, I arranged that in adding a Rigidbody2d to them.

Now, I want to get there Position to call another function, and here is the Problem. I always get the canvas position, Im not able to get the position of the child-objects, all they have is a Rect-Position, when I watch the game via the editor I can see, that the Rect-Position XPos is going down like it should, but I can´t access the position via script…

Do you have any help on that for me?

Ps. I already searched this Forum and found something like a

.GetWorldCorners();

method, but it also responds the same coordinates every time although my graphics are moving…

Greets
Marcus

I’m not really sure, but RectTransform.anchoredPosition or RectTransform.rect in the RectTransform class might be what you are looking for.

UI elements have RectTransforms instead of Transforms, which work a bit differently and the values displayed in the inspector depend on the anchors.

Well, for moving UI, I do suggest using a tweening engine. but, that’s just advice.

Recttransforms that are centered should give you position just like a transform, using either transform.position for world space or transform.localPosition for the position based on the parent. Note that this is for if the script is on the button itself, otherwise, you’ll have to have a reference to the button and get it’s position or localPosition.

I suggest showing some code on what you are doing so we can help you better.

1 Like

RectTransforms are Transforms. So straight up transform.position will work. You then have to jump through a couple of hoops to get it to a reasonable space.

1 Like

Good evening (from my position of view :slight_smile: )
thanks for all the replies and hints. I will check them out soon and let you know, if they were working for me.

Also I will write some code here what I have done, if Im not sucessfull

Greetings
Marcus

Hello,
again it´s me. So thanks again for the help of all of you.
Yesterday I was sitting for hours trying to figure out what went wrong in the script, and today (also through your help) I got the solution within some minutes.

So as Im very new to Unity I made a big mistake in trying to get the position.

I tried both of your suggested functions: RectTransform.rect and transform.position . The mistake I made was, that I added them to a own created method which was not relating to the void Update().

So I thought that I can do a while loop and figure out the movement within that loop without binding it to update…

Sorry for that and thanks again very much for your help.
With both: RectTransform.rect and transform.position it worked clearly after putting it into the right method (void update()) .

Yours Marcus