Why is my y-axis bugging?

Hi, all!

I’m currently working on an assignment from my university and can’t figure this following thing out.

In my project, I have a sphere that follows my mouse. When I touch a cube, it’s selected and I want to perform transformations on it. When I hold the left mouse click, I want to move the cube.

The following script comes from the update() in my sphere:

            Vector3 spherePosition = transform.position;

            // Modify position
            if (Input.GetMouseButton(0))
            {
                Debug.Log("Current Sphere position: " + spherePosition);
                selected[0].transform.position = spherePosition;
            }

The “selected” is a Cube-element.

Now the issue is that, although I’m not moving the mouse, the y-axis is acting weird every other frame. I jumps to much lower values so that I sometimes loose it in the camera fram when I let go from the left mouse click:

I could not help myself with googling so thanks in advance for any tips!

Then you have to provide the code where u are changing the sphere position. You said that it should follow the mouse, where is the code responsible for that?