MoveTowards issue, animating ui.

So im trying to animate a UI element using movetowards - since the objects have images attached to them i cant use transform (although i’ve tried on two objects neither using recttransform, basically im trying to “hide this ui element when not where its relevant, and activate it, sliding it in from the side” - here’s the code

private IEnumerator moveTowardsObject()
    {
        tempObject = GameObject.Find(animatorTarget);
        while (Vector3.Distance(this.GetComponent<RectTransform>().localPosition,
            tempObject.GetComponent<RectTransform>().localPosition) > 1f)
        {
            Debug.Log(this.name+" - "+this.GetComponent<RectTransform>().localPosition);
            Debug.Log(tempObject.gameObject.name+" - "+tempObject.GetComponent<RectTransform>().localPosition);

            this.GetComponent<RectTransform>().localPosition = Vector3.MoveTowards(
                this.GetComponent<RectTransform>().localPosition,
                tempObject.GetComponent<RectTransform>().localPosition,
                animationSpeed * Time.deltaTime);

            yield return null;
        }
        Debug.Log("Reached destination");
        isAnimating = false;
        yield return null;
    }
    /*public void moveTowardsObject() {
        tempObject = GameObject.Find(animatorTarget);
        //uiObjectScript = tempObject.GetComponent<UIObject>();
        RectTransform toTarget = tempObject.GetComponent<RectTransform>();
        
        gameObject.GetComponent<RectTransform>().localPosition = Vector2.MoveTowards(
            gameObject.GetComponent<RectTransform>().localPosition, 
            toTarget.localPosition,
            Time.deltaTime * animationSpeed);

        //Debug.Log("");
        //Debug.Log("Animating to "+uiObjectScript.gameObject.name);

        if (gameObject.GetComponent<RectTransform>().localPosition.localPosition == toTarget.localPosition) {
            Debug.Log("Reached destination");
            isAnimating = false;
        }
    }*/
    public void setTargetAndAnimate(string tempObjectName)
    {
        isAnimating = true;
        animatorTarget = tempObjectName;
        Debug.Log("Starting animating to "+tempObjectName);
        StartCoroutine(moveTowardsObject());
    }

Maybe im doing something wrong here, but as you can see from all the stuff i have commented out, i have tried various ways to do this, looked at several questions and threads on the subject- but i cant seem to make it work, in a nice and clean manner. And while i know my code, unity is a different beast.


Does anyone have a solution? Or is able to help me ? I’ve been working on this for hours, and surely it must be possible- it does NOT throw any exceptions or errors, it just simply does not move the elements what so ever, and the while loop in this specific attempt just continues to run. Again i’ve tried atleast 6 different ways so far. So i figured it was time to ask if anyone knew what was up and how to do this. - thankyou!


Still havent found any good solution… Anyone?
Edit2: so when i run this code- i tried to output the distance between to console - it gives me, that it moves a very small number - no matter what i set animation speed to ?


here is a picture of what i mean -

Please, take a look the the documentation. UI elements should not be moved using position nor localPosition, but with anchoredPosition or offsetMin / offsetMax.