我正在学习教程,但遇到覆盖错误。无法弄清楚问题所在。 CS0115

I was following a tutorial but come across an error with override. Can't figure out the problem. CS0115

代码如下。错误是错误 CS0115: 'LaunchManager.OnPlayerEnteredRoom(Player)': 找不到合适的方法来覆盖。 我是编码新手,所以非常感谢任何帮助!

        public override void OnPlayerEnteredRoom(Player newPlayer)
    {
        Debug.Log(newPlayer.NickName + " joined to " + PhotonNetwork.CurrentRoom.Name + " " + PhotonNetwork.CurrentRoom.PlayerCount);
    }

关键字 override 在子 class 中用于覆盖基 class 中具有相同名称和相同参数的方法的实现。您需要将 virtual 关键字与 base class 的方法一起使用,以便 OnPlayerEnteredRoom 能够覆盖。 示例:

    class PlayerMovement {
        public virtual void OnPlayerEnteredRoom(Player newPlayer){
        // base class implementation 
      }
    }
    class PlayerTypeOneMovement : PlayerMovement {
       public override void OnPlayerEnteredRoom(Player newPlayer){
        // child class implementation 
      }
}

你需要先学习多态,这个概念你才会清楚。

OnPlayerEnteredRoom 是一个 virtual method in MonoBehaviourPunCallbacks.

如果您想使用它,请确保您的 class 继承自该类型,而不是 MonoBehaviourMonoBehaviourPun.


另见 this thread。听起来好像有人遵循了相同的教程,错误是:

I finally figured out the problem. I had a public class Player in the same namespace - so those methods were trying to use that class as their argument type, instead of photon.realtime's Player.

因此请确保您引用的 Player class 如果您也是这样的话!

public class YourClass : MonoBehaviourPunCallbacks
{
    public override void OnPlayerEnteredRoom(Photon.Realtime.Player newPlayer)
    {
        ...
    }
}