How to Find TextMeshPro Objects in Hierarchy and Edit Them?
This is a bit of an oddly specific issue I'm having, but I'm making a small platformer where the platforms are all letters and such. I want the letters to change color when they're jumped on, but can't quite figure out how to get my script to find which letter I'm colliding with. Here's the code I've got so far.
public class LetterScript : MonoBehaviour
{
TextMeshPro currentLetter;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Letter"))
{
Debug.Log("Collision detected!");
currentLetter = (TextMeshPro)FindObjectOfType(typeof(TextMeshPro)); //I want to set currentLetter to the TMPro object I land on so that I can change its color.
Debug.Log("currentLetter is:" + currentLetter.name);
currentLetter.color = new Color32(255, 255, 255, 255);
}
}
}
Answer by GDGames0302 · Feb 13, 2021 at 08:07 AM
Hi. In the parentheses you declared a Collider2d collision. You can do:(This works only if you collided with an object that has text mesh pro, otherwise you will get an error). If it doesn't work, take a look at this "There are two TextMesh Pro components... the normal component is of type TextMeshPro and works with the MeshRenderer and a replacement for the old TextMesh. Then the UGUI component is of type TextMeshProUGUI and works with the CanvasRenderer. Both of these inherit from TMP_Text" Link: https://forum.unity.com/threads/changing-textmeshpro-text-from-ui-via-script.462250/ . If this code doesn't work, try GetComponent TMPro.TextMeshPro.
collision.GetComponent<TMPro.TextMeshProUGUI>().color = .......
Your answer
![](https://koobas.hobune.stream/wayback/20220613025104im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
clone, instantiates 2 of my prefabs instead of 1 1 Answer
I can read this information on inspector by script. 0 Answers
Array of scripts on component 1 Answer
Attaching fbx to Scene on Android 0 Answers