- Home /
How to keep game object aligned with text mesh
I'm trying to keep a counter with a sandtimer icon centered over a buy button. The sandtimer icon is a child object to the counter's TextMesh. The problem is, the sandtimer child can't inherently follow the counter text if it changes in size (e.g. 2d 55m -> 1d), the sandtimer will stay put.
How can I keep them together, while also centered over the buy button?
Comment
Best Answer
Answer by Essential · Jun 03, 2014 at 01:36 PM
This script attached to the counter TextMesh seems to work nicely, if the textMesh text is aligned to the left side.
#pragma strict
// This script keeps the gameObject's position aligned to its textMesh actual size
var textMesh : TextMesh;
var offset = 0.08;
private var rend : Renderer;
function Start ()
{
rend = textMesh.renderer;
}
function Update ()
{
transform.localPosition.x = offset - (rend.bounds.size.x*2);
}
Any other suggestions are welcome though if there's a better way.