SetParrent( ) changes the Y position ?

In my game there is picup and throw funchtion that work on 3D objects like Cubes but when I close and after that I open the project again, that funchtion doesn’t work.Instead of working like it shoul the scipt just change the Y position upwards. In order for it to works again I need to recreate the 3D object again , add the scipt and after that everything its fine. Here is the code :
float throwForce = 1500;
Vector3 objectPos;
float distance;

public bool canHold = true;
public GameObject item;
public Transform tempParent;
public bool isHolding = false;

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

    distance = Vector3.Distance(transform.position, tempParent.transform.position);
   if (distance >= 2f)
   {
       isHolding = false;
   }
   //Check if isholding
    if (isHolding == true)
    {
        transform.parent = tempParent;
        GetComponent<Rigidbody>().velocity = Vector3.zero;
        GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
        

       if (Input.GetMouseButtonDown(1))
       {
           GetComponent<Rigidbody>().AddForce(tempParent.transform.forward * throwForce);
           isHolding = false;
       }
    }
    else
    {
        objectPos = transform.position;
        transform.SetParent(null);
        GetComponent<Rigidbody>().useGravity = true;
        transform.position = objectPos;
    }
}

void OnMouseDown()
{
    if (distance <= 2f)
    {
        transform.SetParent(tempParent.transform);
        isHolding = true;
        GetComponent<Rigidbody>().useGravity = false;
        GetComponent<Rigidbody>().detectCollisions = true;
    }
}
void OnMouseUp()
{
    isHolding = false;
}

Unless the parent is not at cordinates 0,0,0 then is will change the cordanits but not the position.