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.