Home > Enterprise >  Why can't I override Photon virtual functions?
Why can't I override Photon virtual functions?

Time:09-05

This is my code

public class SomeClass : MonoBehaviourPunCallbacks
{
    public override void OnPlayerEnteredRoom(Player newPlayer)
    {
        newPlayer.NickName = "hello";
        return;
    }
}

and then there is the photon file:

public class MonoBehaviourPunCallbacks : MonoBehaviourPun, IConnectionCallbacks , IMatchmakingCallbacks , IInRoomCallbacks, ILobbyCallbacks, IWebRpcCallback, IErrorInfoCallback
{
    public virtual void OnPlayerEnteredRoom(Player newPlayer)
    {
    }
}

My code cannot access the function.

Does anyone know what is wrong with this and what I have to do to fix it?

CodePudding user response:

I suspect a missing namespace.

While MonoBehaviourPunCallbacks is in the namespace Photon.Pun, the type Player is in Photon.Realtime.

So either you need to add a

using Photon.Realtime;

or in case you have your own type somewher called Player as well either add

using Player = Photon.Realtime.Player;

or explicitly use

public override void OnPlayerEnteredRoom(PlaPhoton.Realtime.Player newPlayer)
{
    newPlayer.NickName = "hello";
    return;
}

CodePudding user response:

Write a test class. Mount the 3 scripts in the scene.

public class Test : MonoBehaviour
{
       SomeClass sc
       
       void start()
       {
         sc = GetComponent<SomeClass>()
         sc.OnPlayerEnteredRoom(Player newPlayer)
       }
 }
  • Related