I have a list of buttons arranged in a grid layout inside of a scrollview. I’m attempting to create an arrow in the UI to show what item in this inventory is currently selected. To do this, I’m trying to get the world position of the buttons in the Grid Layout and snap the arrow to it.
Observe the console output at the bottom of the attached gif. When I change my selection, note that the world position’s Y component never changes. This means I can’t use the buttons world position to place an image.

My logging code. Nothing fancy.
GameObject curSelected = EventSystem.current.currentSelectedGameObject;
Debug.Log(string.Concat(curSelected.name, " | WORLD POS: ", curSelected.transform.position, " | LOCAL POS: ", curSelected.transform.localPosition), curSelected);
Worth noting the X component changes as expected, though it’s not represented in the gif. I’ve verified the correct item is being represented in the console.
I would expect my world Y to be different every time I change the button. But it always remains the same, meaning I can’t place my arrow image on it. Is there an explanation for this? How hard can it be to move an image on top of another independent of hierarchy?
Something weird is going on. Check what you are focusing on.
I have a GridLayoutGroup in the main menu of the game I’m working on right now. I dropped your code into my cursor move routine and got exactly what I expected:

Notably, I made sure that for each of my entries, the Button Component (which is what gets focus) is on the root, while other stuff is parented below it. Here is what an entry looks like in the hierarchy (immediately below the GridLayoutGroup):

My setup is a ScrollView, with a GridLayoutGroup inside it’s content panel. The GridLayoutGroup has a content size fitter to make it stretch vertically. I can’t imagine how that’d affect the world position of the buttons though.
Well if it is any consolation so is mine!


I slapped together an editor script to read the world position values from the rect transform in the inspector. It appears to show the correct values. The Y value changes when the row changes (as expected). Gif to demonstrate it’s functionality.

This is all it is.
[CustomEditor(typeof(RectTransform))]
[CanEditMultipleObjects]
public class CustomTransform : Editor
{
public override void OnInspectorGUI()
{
Transform transform = ((Transform)target).transform;
EditorGUILayout.BeginHorizontal();
transform.position = EditorGUILayout.Vector3Field("World Pos", transform.position);
EditorGUILayout.EndHorizontal();
}
}
I then compared what my debug log was reporting vs what was being reported in the inspector.
This was the result.
The Y value reported in the console does not match what is being reported by the inspector.
I confirmed the right button is being selected by having the debug.log selected the curSelected object when selected in the console. This is demonstrated in the screenshot above by having selected the console message, and having it selected the correct button in the hierarchy.
I have no idea of what’s going on here. I’m using transform.position in two different places and getting two different results.
This could be a difference of being under a (different) CanvasScaler element, which I assume your original Grid Layout Group was.
I still don’t know why it wouldn’t show only zeroes for you from the first post above. What exactly are your Canvas and CanvasScaler modes?

All relevant objects are located under this canvas.
Sorry for the bump, but I made some progress and figured it’d be useful to share it.
Turns out the my setup with layout groups isn’t the issue at all. It appears to be some issue with me sampling the position of my objects in the scroll rect. Originally I was scrolling my scroll rect manually by moving the content panel. This is what introduced the incorrect behavior in the OP. Modifying the VerticalNormalizedPosition appears to have resolved that behavior.
Another issue came up though.
When placing the arrow, I’m doing the following.
arrowTransform.position = EventSystem.current.currentSelectedGameObject.transform.position;
However, it introduces this offset that I can’t wrap my head around. Note how the arrow becomes more offset as it scrolls to the top.

The setup is same as before. A grid layout placed inside the content panel of a scrollrect. Is there an explanation for this behavior?
1 Like
Well technically all the UnityEngine.UI source code is available, either from the package or from github, so yes there is an explanation, but it might take a lot of weaseling through layers of inherited objects passing messages between them before you figure it out!
Either way, you might get some valuable intel sniffing around the source. Sorry I didn’t mention that approach earlier.