0

I have a static string variable called "playernamestr" in a class called nameSettings, and am trying to call it in another script. However, its giving me the error as shown in the title.

public class nameSettings : MonoBehaviour
{
    public static string playernamestr;
    public Text playerName;

    void Start()
    {
        playerName.text = playernamestr;
    }
}

Here on line 19 is where I'm trying to call it to put it into a JSON file:

using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameHandler : MonoBehaviour
{

    private void Start() 
    {
        Debug.Log("GameHandler.Start");          

        string json = File.ReadAllText(Application.dataPath + "/saveFile.json");
        PlayerData loadedPlayerData = JsonUtility.FromJson<PlayerData>(json);
        this.transform.position = loadedPlayerData.position;

        GetComponent<CharacterState>().m_Health=loadedPlayerData.health;
        GetComponent<PlayerControllerZ>().count = loadedPlayerData.countNumber;
        GetComponent<nameSettings>().playernamestr = loadedPlayerData.myName; // this line
    }

    private class PlayerData
    {
        public Vector3 position;
        public int health;
        public int countNumber;
        public string myName;
    }

    private void OnDisable()
    {
        PlayerData player = new PlayerData();
        player.position = this.transform.position;

        player.health = GetComponent<CharacterState>().m_Health;
        player.countNumber = GetComponent<PlayerControllerZ>().count;
        player.myName = GetComponent<nameSettings>().playernamestr;

        string json = JsonUtility.ToJson(player);

        File.WriteAllText(Application.dataPath + "/saveFile.json", json);
    }
}

Any idea how to fix this? I know it has something to do with it being static, but can't figure it out even after googling it.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • `nameSettings.playernamestr` ? :) – ProgrammingLlama May 05 '20 at 07:51
  • Also, [first Google result for your exact question title](https://stackoverflow.com/questions/1100009/member-method-cannot-be-accessed-with-an-instance-reference). – ProgrammingLlama May 05 '20 at 07:52
  • 1
    Instance Fields use [InstaneReference].[VariableName]. Static fields use [ClassName].[VariableName]. – Christopher May 05 '20 at 07:57
  • Hey, the `static` modifier means it is a variable of the class itself, rather than an instance of the class. So `nameSettings.playernamestr`, rather than `new nameSettings().playernamestr`. You'll want to google around what the static modifier does. – mrdnk May 05 '20 at 08:05
  • 1
    Maybe learn about object-oriented programming then, before you attempt something as complex as this. An instance of a class is where you create a new "copy" of it (for lack of a better word), using `Classname myObject = new Classname();`, and then `myObject` becomes an indepdendent entity, able to use all the methods and properties defined in `Classname`. Where as a static field is not specific to one instance, instead it's accessed directly via the Classname. You can undoubtedly [find a better explanation online](https://www.google.com/search?q=c%23+object+oriented+programming) though :-) – ADyson May 05 '20 at 08:07
  • 1
    Thank you for your edits. Your question is a lot better now :) – ProgrammingLlama May 05 '20 at 08:09

0 Answers0