- Home /
Script Repositions UI Element in the Wrong Spot
I was originally trying to re-position a UI Image to be in between the player camera and an enemy, but it kept being in the wrong spot, so eventually, I added the following code to OnDrawGizmosSelected:
Gizmos.color = new Color(1, 1, 1);
lockIcon.transform.position = enemyTarget.position;
Gizmos.DrawLine(enemyTarget.position, lockIcon.transform.position);
The line draws, and it has considerable length (it should obviously have 0 length). Note that lockIcon is the GameObject with the Image component attached, and is a child of a Canvas with the World Space render mode, while enemyTarget is the Transform of the enemy game object.
What is going wrong, and how do I fix it?
Will it work when you replace the World-Space-Canvas including the UI Image to a position which is (enemy.position+player.position)/2f ? The problem with the Z axis of the Image is that it's only used as depth, not actual position.
Some extra info which might helpt later on: The near and far planes still consider it a world position so if we consider the far plane to be 1000f and you place the camera at (0,0,0)(facing z+) and the Canvas at (0,0,1) and the icon at (0,0,1001), you will actually not see the icon even though it's should be 1 unit in front of the camera on the flat face of the Canvas.
Your answer
![](https://koobas.hobune.stream/wayback/20220612103019im_/https://answers.unity.com/themes/thub/images/avi.jpg)