-1

Unity says that my Code has a error:

Member 'PlayerSaving.levels' cannot be accessed with an instance reference; qualify it with a type name instead PlayerData.cs(13,17)

Here the code:

[System.Serializable]
public class PlayerData
{
public int level = 0;
public int coins = 0;

public PlayerData (PlayerSaving player)
{
    level = player.levels;
    coins = player.savedCoins;
}
}

and

public class PlayerSaving : MonoBehaviour
{
public static int levels = 0;
public static int savedCoins = 0;

void Update()
{
    if (levels != Endlevel.level)
    {
        levels = Endlevel.level;
    }
    if (savedCoins != SC_2DCoin.totalCoins)
    {
        savedCoins = SC_2DCoin.totalCoins;
    }
}

public void SavePlayer()
{
    SaveSystem.SavePlayer(this);
}

public void LoadPlayer()
{
    PlayerData data = SaveSystem.LoadPlayer();

    levels = data.level;
    savedCoins = data.coins;
}
}

I need for my variables the static to access with more scripts the variables.

MarcUs7
  • 25
  • 5

1 Answers1

1

if you want to use levels field in PlayerSaving class as static member, you must access it directly from class not from an instance object of that class. like this example :

public PlayerData (PlayerSaving player)
{
    level = PlayerSaving.levels;
    coins = PlayerSaving.savedCoins;
}

or make the fields instance member

public class PlayerSaving : MonoBehaviour
{
// remove static keyword
public int levels = 0;
public int savedCoins = 0;
...
}