Using Input.mousePosition to move panel to mouse position. Only works properly first time


I’m using Input.MousePosition to move a panel to the position of the mouse like so:

 Vector2 pos = new Vector2(Input.mousePosition.x + (inventory.optionsMenuRect.rect.width / 2) + 2, Input.mousePosition.y);
        inventory.optionsMenu.transform.position = pos;

How this occurs is I click on an item within my inventory the window is moved to the position of the mouse and then set active. When the mouse leaves the window it will go back to being inactive. Everything works fine the second time. However, when I try to do it any number of consecutive times sometimes the window will stay in the previous positions, the buttons inside the panel will not work anymore and the window will move but just not to the correct place.

Any idea why this is happening?

I don’t know what was initially causing the issue. However, I have fixed it. The solution for me was moving the window out of view by setting up a preset Transform where the window would be when it’s not needed. When it’s needed it is then moved to the MousePosition.