unity3d 存在单位错误CS 0176:无法使用示例引用访问成员“PlayerSaving.levels”;改为使用类型名称限定它[duplicate]

zfciruhq  于 2023-03-09  发布在  其他
关注(0)|答案(1)|浏览(186)
    • 此问题在此处已有答案**:

Member '' cannot be accessed with an instance reference(11个答案)
cannot be accessed with an instance reference; qualify it with a type name instead(1个答案)
11天前关闭。
Unity说我的代码有错误:
无法使用示例引用访问成员"PlayerSaving. levels";用类型名而不是PlayerData.cs来限定它(13,17)
下面是代码:

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

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

以及

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;
}
}

我需要为我的变量静态访问更多的脚本变量.

yhxst69z

yhxst69z1#

如果你想使用PlayerSaving类中levels字段作为静态成员,你必须直接从类中访问它,而不是从类的示例对象中。

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

或使字段成为示例成员

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

相关问题