Text with fixed size in viewport

Hi, i’m trying to do my Text Mesh always have the same size in the viewport. However, something weird is happening. The text size change all the time, with a strange behavior. It’s get bigger and than smaller, repeatedly.

My code is this:

void Update()
{

    TextMesh textMesh = _billboard.GetComponentInChildren<TextMesh>();
    TextBackground textBcg =_billboard.GetComponentInChildren<TextBackground>();
			
    if(textMesh)
    {
        _targetViewportHeight = 40;
        textMesh.fontSize = 40;
        Vector3 minProjPoint = _camera.GetComponent<Camera>().WorldToScreenPoint( textMesh.collider.bounds.min );
        Vector3 upperLeftCorner = new Vector3( textMesh.collider.bounds.min.x, textMesh.collider.bounds.max.y, textMesh.collider.bounds.max.z );
        Vector3 maxProjPoint = _camera.GetComponent<Camera>().WorldToScreenPoint( upperLeftCorner );
				
        float scaleCorrection = _targetViewportHeight / Mathf.Abs( maxProjPoint.y - minProjPoint.y );
        _billboard.transform.localScale = new Vector3( scaleCorrection,    scaleCorrection, scaleCorrection );
    }
}

This is what I get from my code, in play mode.

The Text Mesh get bigger and then smaller, repeatedly. I don’t know how to fix this, or what I’m doing wrong.

What seems to me (via debug tests), is that changing the scale doesn’t affect the bounds. Bounds are not been update. =S

Thanks for any help.

Hi, I could do this work with the following code:

void Update ()
{

    TextMesh textMesh = _billboard.GetComponentInChildren<TextMesh>();
    TextBackground textBcg = _billboard.GetComponentInChildren<TextBackground>();
			
    if(textMesh)
	{
	    _targetViewportHeight = 40;
        Vector3 minProjPoint = _camera.GetComponent<Camera>().WorldToScreenPoint( textMesh.renderer.bounds.min );
	    Vector3 upperLeftCorner = new Vector3( textMesh.renderer.bounds.min.x, 
										textMesh.renderer.bounds.max.y, 
										textMesh.renderer.bounds.max.z );
        Vector3 maxProjPoint = _camera.GetComponent<Camera>().WorldToScreenPoint( upperLeftCorner );
		
	    float scaleCorrection = _targetViewportHeight / Mathf.Abs( (maxProjPoint.y - minProjPoint.y) );
				
	    _billboard.transform.localScale = new Vector3(_billboard.transform.localScale.x * scaleCorrection, _billboard.transform.localScale.y * scaleCorrection, _billboard.transform.localScale.z * scaleCorrection);
	
	}
}