NullReferenceException for InputField

I keep getting NullReferenceException when trying to get the text from a field on button click. I also tried using TextMeshPro Input Field, but same issue.

I followed this video:

So I added my script to the ‘canvas’, then added a field that looks like this:

public InputField playerName;

public void OnButtonClick()
{
    Debug.Log(playerName.text);

Then I dragged the field into the script:

But still I get the error (The line number is for the line playerName.text (6 above)):

I solved my issue, forgot I had added the Script to an invisible object. Adding the field to that MainScript worked