TextMeshProUGUI Errors when displaying anything but direct string. [solved]

Hi!

I keep having issues with TextMeshProUGUI. I have set it up correctly:

public class MainCode4 : MonoBehaviour
{
    public TextMeshProUGUI displaytext;
    private string test1;

void Start(){
        test1 = "say / type";
        displaytext.text = test1;

Up until here everything works; the TextMeshProUGUi is “say / type”.
However, I get an error immediately when I run program and try to display it anything else, like a .name.

For example:

            if (Physics.Raycast(transform.position, fwd, 100)){
                Transform objectHit = hit.transform;
                test1 = objectHit.name;
                displaytext.text = test1;

Returns: “TextMesh Pro NullReferenceException: Object reference not set to an instance of an object”.
The text mesh is added correctly in the inspector (of course as the first code works, displaying everything correctly).

Help is appreciated!

Use code tags to post code, and copy the full text of the error out of the console (including the lines below it which include things like line numbers).

I suspect this doesn’t actually have anything to do with TextMeshProUGUI or the string. It doesn’t look like you assign a value to “hit” anywhere (usually it’d be one of the parameters in the Raycast() call). So objectHit will be null, and trying to get a null object’s name causes a NRE.

Issue was that some assets were missing; Import TMP Essential Resources solved it.