简体   繁体   中英

Why can't I override Photon virtual functions?

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM