Object reference not set to an instance of an object Error (767606)

I have this code used to save input from a InputField. When I press the button that executes this script I get the error “Object reference not set to an instance of an object” at line 13. I have used a similar get input script in other parts of the project so I am not sure what the problem is here. The button is pressed after I enter my input into the input field.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class attackInputField : MonoBehaviour
{
    public InputField WhoToAttack;
    public static string victim="";

    public void save()
    {
        victim = WhoToAttack.text;
        Debug.Log("victim chosen");
        PlayerPrefs.SetString("textTextKeyName", victim);
    }
}

It seems like you did not set WhoToAttack.text to anything, so it throws the null pointer.

You can check if WhoToAttack.text actually contains anything before using it:

public void save()
{
     if(WhoToAttack.text != "")
     {
          victim = WhoToAttack.text;
          Debug.Log("victim chosen");
          PlayerPrefs.SetString("textTextKeyName", victim);
     }
     else
     {
     Debug.Log("Text is empty!");
     }
}