- 此问题在此处已有答案**:
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;
}
}
我需要为我的变量静态访问更多的脚本变量.
1条答案
按热度按时间yhxst69z1#
如果你想使用
PlayerSaving
类中levels
字段作为静态成员,你必须直接从类中访问它,而不是从类的示例对象中。或使字段成为示例成员