Hovering text over objects

I’m trying to get my labels to stay above my objects but if I’m looking directly down or up at the object the text will be positioned too much below or above the object, same thing if I zoom the camera too far in and out. Also when attempting to attach this script to other objects the labels go all over the place and dont remain over the other objects correctly.

	public Vector3 offset = Vector3.up;
	public Vector3 screenPos;

	void Update () {

		screenPos = Camera.main.WorldToScreenPoint(transform.position + offset);
	
	}

	void OnGUI ()
	{      
		
		GUI.Label(new Rect(screenPos.x - 40,screenPos.y,100,50),"test1", textStyle);
		GUI.Label(new Rect(screenPos.x - 80,screenPos.y + 20,300,50),"test2test2test2", textStyle);
	}

You’re offset should not be added to the 3d position. You’re just creating a point over the box in the 3d world, not necessarily on the screen. You should add the offset in the OnGUI() function.

screenPos = Camera.main.WorldToScreenPoint(transform.position);

int offset2D = //some value;
void OnGUI ()
     {      
         
         GUI.Label(new Rect(screenPos.x - 40,screenPos.y+offset,100,50),"test1", textStyle);
         GUI.Label(new Rect(screenPos.x - 80,screenPos.y+offset + 20,300,50),"test2test2test2", textStyle);
     }