2D element displaying onscreen in wrong place. Why?

Hello, Apologies for the long post, bear with me!

I’m trying to make a minimap for a racing game but I’m having trouble getting the 2d element to appear onscreen in the correct place.

Here is the script I’m using to determine the correct place onscreen for the 2d element. (Hereby called playerLocation)

    public GameObject minimapImage;
    public Vector2 playerMinPos = new Vector2( -500f, -30f), playerMaxPos = new Vector2(1500f, 130f), uiMinPos = new Vector2(-400f, -30f), uiMaxPos = new Vector2(400f, 40f);

    public float playerXRange, playerYRange, playerXNormal, playerYNormal, uiXRange, uiYRange;

    public float currentX, currentZ;

    public Vector3 uiPosition;

	// Use this for initialization
	void Start () {
        playerXRange = playerMaxPos.x - playerMinPos.x;
        playerYRange = playerMaxPos.y - playerMinPos.y;

        uiXRange = uiMaxPos.x - uiMinPos.x;
        uiYRange = uiMaxPos.y - uiMinPos.y;
    }

    // Update is called once per frame
    void Update () {
        playerXRange = playerMaxPos.x - playerMinPos.x;
        playerYRange = playerMaxPos.y - playerMinPos.y;


        currentX = gameObject.transform.localPosition.x;
        currentZ = gameObject.transform.localPosition.z;

        playerXNormal = (currentX - playerMinPos.x ) / playerXRange;
        playerYNormal = (currentZ - playerMinPos.y ) / playerYRange;


        uiPosition  = new Vector3( uiMinPos.x + ( uiXRange * playerXNormal ),
                                   uiMinPos.y + ( uiYRange * playerYNormal ) + 10f,
                                   0f );
        minimapImage.transform.localPosition = uiPosition;
    }

minimapImage is the minimap player location sprite.

playerMinPos/playerMaxPos Is the top left, and bottom right of the players allowed area in world space.

uiMinPos/uiMaxPos the same as above but the UI top left/bottom right in screen space.

playerXRange/playerYRange is calculated from the min/max positions as the width/height of the playable area.

playerXNormal/playerYNormal is the normalised values (from 0 to 1) used to determine the UI position from the UI Range.

uiXRange/uiYRange as the player range, but for the UI.

currentX/currentZ currently only public to see what values are set as the player location.

uiPosition currently only public to see what values are set as the UI location.

.

OK then, based on the above, the formula seems to work. When moving the player all the way to the left, uiPosition.x reads as -400 as it should, and when all the way to the right, it reads as 400, again, as it should.

.

However, when looking at the inspector for the playerLocation object, it reads as a different value. (For example, when uiPosition.x reads as 332.7953, the gameObject position reads as 317.6499. (-396.8521 reads as -460.6353 and so on)

.

Everything is being shifted over to the left, but if I add say 40f to the x position, its correct towards the right of the minimap, but till wrong towards the left.

.

What am I doing wrong!!

Never mind. Today when running the editor, it all worked as expected.

I have no idea why the same code wouldn’t work one day, then work the next.