How to change text in new UI?

I’m using the new Text component in the UI and I’m changing the text at runtime using:

GetComponent<Text>.text = "new text";

but the profiler shows a huge spike in CPU everytime I change the text. Is there a more appropiate way to do it?

Instead of using GetComponent each time(you do take a small hit each time you use GetComponent) get a reference to the Text component if possible and reuse the reference:

example:

c#
using UnityEngine.UI;

public class SomeClass : MonoBehaviour
{
  Text myText;

  public void Start()
  {
    myText = GetComponent<Text>();
  }

  public void UpdateText()
  {
    mytext.text = "Some new string value";
  }
}

unityscript
import UnityEngine.UI;

var myText: Text;

function Start()
{
  myText = GetComponent.<Text>();
}

function UpdateText()
{
  myText.text = "Some New String Values";
}

I found out what was causing the CPU spike. It was the dynamic fonts, once I changed the fonts to static, everything started to run smoothly. The dynamic font + Best Fit on the Text was causing the font to cache everytime I changed the string in the text.