Help us understand the problem. What is going on with this article?

PUN2 でUserId がnull になっている時の対処法

環境

  • Windows 10
  • Unity 2019.2.19f1
  • Photon2 (Photon Unity Networking 2 2.16)

症状

public class NetworkInterface : MonoBehaviourPunCallbacks
{

    private void Start()
    {
        PhotonNetwork.ConnectUsingSettings();
        PhotonNetwork.JoinOrCreateRoom(_config.RoomName, new RoomOptions(), TypedLobby.Default);
    }

    public override void OnJoinedRoom()
    {
        Debug.Log("Room Max number: " + PhotonNetwork.CurrentRoom.MaxPlayers); 
        Player[] player = PhotonNetwork.PlayerList;

        // プレイヤー名とIDを表示.
        for (int i = 0; i < player.Length; i++)
        {
            Debug.Log((i).ToString() + " : " + player[i].NickName + " ID = " + player[i].UserId);
        }
    }
}

上記のコードでルームに数人が入る。最後に入室した人の場合、PhotonNetwork.PlayerList に全員登録されているはずなので、NickName とUserId がすべて入っているはずだが、UserId が自分のもの以外null になっている。

対策

RoomOptions を生成するときの、デフォルト値のままではなく、

public class NetworkInterface : MonoBehaviourPunCallbacks
{

    private void Start()
    {
        PhotonNetwork.ConnectUsingSettings();
        RoomOptions options = new RoomOptions();
        options.PublishUserId = true; // ★お互いにユーザIDが見えるようにする。
        options.MaxPlayers = 3; // ★最大人数もきちんと定義しておく。
        PhotonNetwork.JoinOrCreateRoom(_config.RoomName, options, TypedLobby.Default);
     }

    public override void OnJoinedRoom()
    {
...
    }
}

RoomOptions のPublishUserId はデフォルト値がfalse のようなので、true に変更してJoinOrCreateRoom すると意図通りUserId が見えるようになった。

azarashin
Unity で色々作りますが、レーザーカッターで筐体作ったり回路作ったり、 機械学習したり、いろいろやってます。
https://www.pit-creation.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした