Get GameObject's position in global screen space. How?

Quick question…

I have a User Interface I am working on. I have my Canvas set to “Screen Space - Overlay”

Within that UI, I have a GameObject nested many levels deep inside other game objects. I want to find the Vector3 transform (it’s location) of that specific object in Global Screen Space.

Currency if I try and print out the objects position :

Debug.Log(myObject.gameObject.transform.position)

I am getting :
(12.3, 254.0, 0.0)

Yet this GameObject is roughly in the center of the screen. I am assuming that’s it’s position relative to it’s parent.

How can I get it’s position relative to the canvas screen (which is set to 1920x1080)

I’m hoping to get a position of something like
(950, 500, 0.0)

Thanks!!

Also, UI: How can I find a world position on the canvas?

Ya, I tried that before and it doesn’t appear to work as expected :

this :

Debug.Log(this.timeLeftTitle.gameObject.transform.position);

prints out
(12.3, 254.0, 0.0)

And this :

Debug.Log(Camera.main.WorldToScreenPoint(this.timeLeftTitle.gameObject.transform.position));

prints out :
(0.0, 0.0, 0.0)

What am I missing?

I feel like ‘WorldToScreenPoint’ or anything like that is the wrong method. At the end of the day, I just want to convert local screen space to global screen space. I’m struggling to locate the answer on google… hoping someone knows the answer :slight_smile:

Worst case scenario I loop through a game objects parents (recursively to the root) and calculate where it is in the global screen space… but there must be a method already provided to do this. I can’t seem to find the answer anywhere :frowning:

I don’t think it’s possible. Sorry

this is what I did and it seems to work

private void FixedUpdate()
{
GameObjectPosition = Camera.main.WorldToScreenPoint(transform.position);
}```

hope this helps

Bro, I feel you. Could you finish your worst-case scenario method?
I’m also that far to write a recursive method to find position, but am really afraid of all the unknwos lurking doing that.

Btw. in most cases

Camera.main.WorldToScreenPoint(transform.position);

is fine. However, in some constellations of pivots and/or anchors it seems to produce crap, just like

transform.InverseTransformPoint(origin.position)

Sorry to necro, but as there’s no solution here:

transform.position does give the global position on the canvas.

Ex: If you had another object nested deep within others, and then one at the top level. Setting it’s transform.position to the one nested deep would place it right on top of it.

Keep in mind the Anchors and Pivots matter, so for best results set them to top left, or bottom left.

Currently having this issue.

WorldToScreenPoint only works on “Screen Space - Camera”. I need a solution that works with “Screen Space - Overlay”.

EDIT:
This worked for me.

Vector2 position = gameObject.transform.position;
bool withinBounds = RectTransformUtility.RectangleContainsScreenPoint(myRectTransform, position); //missionEditor.selectionRectTransform.rect.Contains(position); //IsPointInRT(position, missionEditor.selectionRectTransform, node);

I ignored this option because I thought you had to supply the camera (which isn’t possible while using overlay) but actually you don’t need to add the camera as third option when you’re using overlay.