Search Unity

Change Text Mesh Pro Text with trigger

Discussion in 'Daydream' started by hjcap1, Apr 30, 2018.

  1. hjcap1

    hjcap1

    Joined:
    Apr 24, 2018
    Posts:
    2
    Hello,

    I'm trying to change the text of a TextMeshPro component which is attached to a canvas in front of the camera.

    I would like it to change when the player interacts with a collide. I have a basic idea of how to set this up but cannot get it to work.
     
  2. millefoliumink

    millefoliumink

    Joined:
    Aug 28, 2014
    Posts:
    139
    To Collider GameObject add Event Trigger, add Pointer Click (or Pointer Enter depending on your preference)

    In Assets window, right-click mouse Create > C# Script, drag this script on to the Collider GameObject.

    The content of script should look something like this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class ChangeText : MonoBehaviour {
    7.  
    8. public TextMeshProUGUI textBox;
    9.  
    10. public void OnPointerClick()
    11. {
    12.     textBox.text = "This is the new text";
    13. }
    14.  
    15. }
    16.  
    In the Event Trigger under Pointer Click, drag the Collider GameObject (with the script attached) to the space with "None(Object)" written in it, from the dropdown list next to "Runtime Only" select the script name "ChangeText" and then the name of the void you want to action "OnPointerClick ()".

    That should do it.