Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question An object reference is required [...]

Discussion in 'Getting Started' started by vendaar, Apr 19, 2021.

  1. vendaar


    Dec 15, 2012
    I'm running into an error when I want to submit a players name to a different scene using a button.

    Scene 1: Menu with InputField and submit-Button. On Buttonpress: Game gets loaded and name should get set.
    Scene 2: Actual Game, when GameOver(); transition to scene 3
    scene 3: Highscores

    Im getting the error "Assets/Scripts/AddNew.cs(11,25) error CS0120: an object reference is required for the non static field, method or property "Playername.SavedName" in my script thats supposed to get score and username when submitting the highscore in scene 3.

    AddNew.cs <- adds the score in scene 3
    Code (CSharp):
    1. void Start()
    2. {
    3. int score = ScoreScript.scoreValue;
    4. string username = PlayerName.SavedName;
    5. Debug.Log(score);
    6. Debug.Log(username);
    7. Highscores.AddNewHighscore(username, score);
    8. }
    PlayerName.cs <- Submit() called when pressting the button in scene 1
    Code (CSharp):
    1. public Inputfield myInput;
    2. public string SavedName = "":
    4. public void Submit()
    5. {
    6. string SavedName = myInput.text;
    7. }
    Can anyone point me in the right direction?