无法访问静态变量

Cannot access static variable

我正在尝试传递一个包含双精度数字的静态 class,这是游戏货币,因为它必须在所有场景中可用(统一编码),这就是我看到的方式轻松访问它。 我想在这里做的是在我的保存系统的一部分 playerdata 脚本中设置静态值,所以静态值一旦改变,就可以在场景之间保存。

这是在播放器数据脚本中

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

[System.Serializable]
public class PlayerData
{
   public double money;

  public PlayerData (GameController player)
{
    //This is where I get the error Member 'GameController.money' cannot be accessed with an instance 
    //reference; qualify it with a type name instead
GameController.money = player.money;  

}
}

这是在 GameController 脚本中

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.UI;
  using UnityEngine.SceneManagement;
  using System;
  public class GameController : MonoBehaviour
  {
  static public double money = 1;
  }

因为该值是静态的,所以您不需要传递 GameController 的实例。

在您的示例中,您传递的参数是 GameController 类型,刚刚命名为 Player。 因此,当您说 player.money 时,它是 GameController 的一个实例,它试图引用一个 GameController 类型的静态变量。我怀疑您打算复制 playerData ( this ) 钱的价值,例如:

GameController.money = this.money;

或只是简化

GameController.money = money;

您可以在 setter 货币变化时执行此操作。