How do you change the text of a Text Mesh Pro with an OnTriggerEnter
using other.CompareTag
?
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Answer"))
{
}
}
How do you change the text of a Text Mesh Pro with an OnTriggerEnter
using other.CompareTag
?
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Answer"))
{
}
}
You need to get component from your “other” object (if your desired target component is in that object):
// in your method:
var myComponent = other.gameObject.GetComponent<MyComponentType>();
Then check if you got the component, and do something with it.
if (myComponent != null)
{
// do something
}
In this case, probably something like:
var textComponent = other.gameObject.GetComponent<TMPro.TextMeshProUGUI>();
textComponent.text = "foobar";
@eses thank you for your help.
A NullReferenceException is appearing, stating the object reference not set to an instance of an object this is appearing when the object hits the trigger
with that being the hierarchy it is trying to access, Fruit_Base being tagged ‘Answer’ and the ANSWER in the canvas being the TextMeshPro I am trying to change. Would something need to change with the code?
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Answer"))
{
var textComponent = other.gameObject.GetComponent<TMPro.TextMeshProUGUI>();
textComponent.text = "foobar";
}
}
Got it working. Thank you so much @eses !!