UI GameObject (Button prefab) randomly changing Z axis RectTransform when instantiated

I was trying to instantiate a prefab button inside a child of canvas. It’s x and y axis are staying constant but Z axis is acting weird. It becomes -800 for some reason and I can’t figure out why. Here is the code:

And when I press play, here is the result:

as you can see, even though I set the rectTransform to something else, it still becomes -800 (I want it to be 0 or anything I want.). Any help would be appreciated. Thank you so much and take care.

EDIT: the second pic got corrupted but you can look at it at the bottom of the post. Thanks

check localPosition of the gameobject, in your case try to set gameobject.localPosition.z = 0;
Hope it helps you solve your issue!