Hello, I’m pretty new and I have a weird problem which I can’t seem to solve.
I want to have objects in my game that I can click and show their description for like 2 seconds or so.
Great! I made a TextMesh and attached it to a game object (along with a MeshRenderer of course), didn’t modify anything, doing all from the script. And here’s how the script looks:
MeshRenderer meshRenderer;
TextMesh textMesh;
float displayTimeLeft;
public string description;
public float displayDuration;
// Use this for initialization
void Start()
{
meshRenderer = GetComponent<MeshRenderer>();
textMesh = GetComponent<TextMesh>();
textMesh.characterSize = 0.18f;
textMesh.alignment = TextAlignment.Center;
textMesh.anchor = TextAnchor.MiddleCenter;
textMesh.text = description;
// Make sure text will be visible over player
meshRenderer.sortingLayerName = "Player";
// Move text upwards
textMesh.transform.Translate(0f, 0.75f, 0f);
// Disable the renderer
meshRenderer.enabled = false;
}
// Update is called once per frame
void Update()
{
// Test for input
if (Input.GetMouseButtonDown(1) && meshRenderer.enabled == false)
{
// Enable the renderer for the display duration
displayTimeLeft = displayDuration;
meshRenderer.enabled = true;
}
// If it's being rendered, count down
if (meshRenderer.enabled)
displayTimeLeft -= Time.deltaTime;
// Stop rendering the text if time ran out
if (displayTimeLeft < 0f)
meshRenderer.enabled = false;
}
Alright, I click the object, the description is shown for 1.8 seconds. Then, I created a prefab and added another object.
This is where it happens :(. If I have 2 of those game objects in my world, when I click to show the description, the description of ALL of the objects is shown.
There might be an easy solution of just clearing the Text of the TextMesh but I don’t find that efficient. Can I work something out using the renderer as I am trying to do at the moment? I tried a lot of ways but still can’t figure it out. Does a single renderer render all of the TextMeshes or what?
Here’s how the project looks like if it helps…
Also, what happens if I click one plant: