TMP set text script isn't working, how to fix it?

 gtext.GetComponent<TextMeshPro>().SetText("Difficulty (Easy)");

The code above the text is what I’m using, I tried .text but it didn’t work, no error in the editor, but when I play it I get a NullReferenceExpection error. If you answered, thanks for the answer.

A NullReferenceException suggests that either gtext has no value or there is no TextMeshPro component attached to it.

Steps to fix it:

As @Antistone pointed out, you have a null reference. Here is how to fix it:

Step 1. Identify what is null. This might require breaking a multiple dot-dot-dot nightmare like that line into a series of discrete steps so you can see exactly what is null, in this case either gtext or the lack of a TextMeshPro object to get.

Step 2. Figure out why it is null. Is something missing? Is something misconfigured?

Step 3. Fix it so it is not null, or change the code to accommodate nulls gracefully.

you probably are trying to change a TMP text in a UI canvas, and if that is the case than you need TextMeshProUGUI instead of TextMeshPro, which is for TMP text in 3d objects.

Note that both of those classes inherit from TMP_Text, so except in the unusual case that you need some functionality that is unique to one or the other, your variables can all be of that type for simplicity.

Ok thanks!