Can't enable/disable the rendering of a SINGLE text mesh

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:

If I read your code correctly (it’s easy to miss things when just skimming) I know where the problem is. Input.GetButton and those other functions are explicitly being called simply when you click/press button etc. It knows nothing about where you clicked. You need to code the logic that says “if click ON THIS OBJECT do this”. Right now you are missing the caps part of the logic.

There is no right way of doing that, so I’ll just point you to some functions you might find of interest: Camera.ScreenToWorldPoint , ScreenPointToRay , Physics.2D.Raycast. Or maybe you want to have the cursor as a gameobject in your scene and check for things happening in OnTriggerEnter etc.

1 Like

I actually have no words to describe how dumb I feel for not realizing the obvious mistake.
Thank you a lot!

No problem :slight_smile: Learning things making us feel dumb is quite ironic, isn’t it. Good luck on your project!