change a variable inside a class, but gives error "Null reference exeption"

I want the player to be able to change their name, so I made a class called “Player”, that only contains the string “name”, in one script. And then tried to create a player instance and change that value with an Input field.
However when the Method UpdateName() is used the console shows an error “NullReferenceException: Object reference not set to an instance of an object” at line 18.

i know I could just use a string variable inside of MenuManager but I prefer to use classes because I want to add even more variables to the class, and I think it will be generally smoother for programming.

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

public class MainMenuManager : MonoBehaviour
{
    public static Player player1;
    public InputField p1;

void Start()
{
p1 = GameObject.Find("Player1Name").GetComponent<InputField>();
}

public void UpdateName()
    {
player1.name = p1.text;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

    public class Player
{
public string name;
}

Hi,
The problem is that you are not initializing player1 so it is an empty reference (null from your error). If you change public static Player player1; into public static Player player1 = new Player(); everything will work fine. Now under player1 there is an object on which you can work.

As @Immortus1 suggested he’s true but ur intentions seems to be different here, instead of creating new object of player class everytime u start the game as public static Player player1 = new Player(); , reference the class u already have and so u can manipulate all the variables u create next inside Player class. U could use an Singleton instead:


Player class:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player
 {
       public string name;
       public static Player instance;        

       void Awake()
       {
           instance = this;
       }
 }

and then reference the Player using its instance as in the blow script:

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class MainMenuManager : MonoBehaviour
 {
     //u dont need this refrence if u use direct instance reference from Player class
     public static Player player1;

     public InputField p1;
 
     void Start()
     {
         //optional refrence if ur not using my recomended method of directly using instance of Player
         player1 = Player.instance;

         p1 = GameObject.Find("Player1Name").GetComponent<InputField>();
     }
 
     public void UpdateName()
     {
        player1.name = p1.text;

        /*or simply use player class instance to get text from it instead or 
        creating an another static reference in MainMenuManager class that 
        will get u the same result as "player1.name = p1.text;" as below*/

        //Player.instance.name = p1.text;
     }
 }

The all idea revolves around u have single player in scene although u can make more singletons but maintaining them is the cost! Good Luck! @codeCosmic