strange behaviour with simple code

if(healthbar.transform.position.y< 158.0 && input.GetKey(KeyCode.KeypadPlus)){
healthbar.transform.position.y+=1*Time.deltaTime;}

I want the bar to stop moving up when trasfnorm.position > 158. In this example the bar goes up even if the transform gets bigger Y value than 158.whats going on? health bar Is UI image. and the var i set is UI.image I see in inspector as Y goes way beyond 158 even if it should have stopped

Sorry I do not have an answer, but are you actually able to manipulate the y position of an object directly like that in java script? I know in c# you have to use this gameObject.transform.position = new Vector3 ( gameObject.transform.position.x, gameObject.transform.position.y + 1 );

Yes. the object moves up via the script the problem is that the if statment should have stop the movment of the object when the condition is met .
when I use same script with healthbar as var GameObject but with difrent object( not UI image) the code works as it should have

thre might me some conection to the fact that the object I want to move is UI image and it’s in canvas. It’s UI element so the propery might not be read corectly but for debug it works just fine…

i think because the new ui scales to whatever screen res there is. so the actual sizses are constantly changing. you should use rectTransform.sizeDelta to manipulate the rect instead.

or anchoredPosition instead of position

I’m guessing this is a local vs. world position problem. The position displayed in the transform window is the object’s local position, while transform.position gives you its world position. And if you’re in the GUI, the RectTransform adds a whole other level of complication (and you should probably use anchoredPosition) and the values shown in the RectTransform inspector are often completely different from the values you get in script (the ones in the editor are more convenient for us fleshy meatbags, the ones in the script are the “real” values). (You can see the real values by switching the inspector to debug mode using the widget at the top right of the inspector)