Trying to move the UI pointer along a UI image

I have a red pinhead which is supposed to move along a image.

All the way to the end…

My approach is to change the X Position in the Rect Transform over a Lerp. But I am failing badly. Here is my script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PinHeadMover : MonoBehaviour
{
    float Initial = 25.5f;
    float Final = 328.5f;
    private RectTransform Rect;

    
    int ID;

    float yPos;


    private DistanceTracker _dTracker;
    // Start is called before the first frame update
    void Start()
    {
        _dTracker = GameObject.Find("Distance Counter").GetComponent<DistanceTracker>();
        ID = _dTracker.GetInitialDistance();                //Getting Initial Distance..
        Rect = GetComponent<RectTransform>();

        yPos = Rect.transform.localPosition.y;              // trying to store yPos of the pinhead
        
    }

    // Update is called once per frame
    void Update()
    {
        //Debug.Log("YO!!!");
        Debug.Log(Rect.transform.localPosition);


        int currentDistance = _dTracker.GetCurrentDistance();
        //Debug.Log(currentDistance);

        Rect.transform.localPosition = new Vector3(Mathf.Lerp(Initial, Final,(ID - currentDistance)/ID),yPos,0f);
    }
}

you are overenginering a slider. create an slider, set the interactable property to false and set the disable color alpha to 1. then you just need to change the slider background image and handler. you can access the value property of the slider by code, for setting the slider pointer position.

Anyways I also tried this method… using

Rect.anchoredPosition

and it worked. This script is attached to that PINhead UI element

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PinHeadMover : MonoBehaviour
{
    float Initial = 25.5f;
    float Final = 328.5f;
    private RectTransform Rect;

    
    int ID;

    float yPos;


    private DistanceTracker _dTracker;
    // Start is called before the first frame update
    void Start()
    {
        _dTracker = GameObject.Find("Distance Counter").GetComponent<DistanceTracker>();
        ID = _dTracker.GetInitialDistance();                //Getting Initial Distance..
        Rect = GetComponent<RectTransform>();

        yPos = Rect.anchoredPosition.y;              // trying to store yPos of the pinhead
      
        Initial = Rect.anchoredPosition.x;
        
        Final = Rect.anchoredPosition.x + 303;
        
        
    }

    // Update is called once per frame
    void Update()
    {
      


        int currentDistance = _dTracker.GetCurrentDistance();
        

        float result = 1 - (float)currentDistance / ID;
        

        Rect.anchoredPosition = new Vector2(Mathf.Lerp(Initial, Final,result),yPos); //(ID - currentDistance)/ID)


    }
}