Search code examples
c#unity-game-enginephotonphoton-pun

I making a multiplayer unity game for this reason i used photon pun but i try PhotonNetwork.JoinOrCreateRoom("room"); dont work


Sorry for turkish language

this the problem


Solution

  • PhotonNetwork.JoinOrCreateRoom takes four parameters: 3 required, 1 optional. Along with specifying the name of the room, (which you did, "oda1"), you must also pass a RoomOptions instance (which contains settings about the room), and a TypedLobby instance (a lobby is a set of rooms).

    Creating a RoomOptions instance is easy enough in Photon, you can just use new RoomOptions(). This is where you will set any room-specific information, such as the maximum number of players per room, via the MaxPlayers property. Pass this as the second parameters into JoinOrCreateRoom

    The TypedLobby instance is a bit more complicated, but as you are just starting out, using TypedLobby.Default is good enough. This just means that it'll use the default lobby for your game. Set this as the third parameter.

    The fourth parameter, string[] expectedUsers, is entirely optional. It is only used when multiple people will join a room at once, like using a party-up system. Do not pass anything right now.

    The final code should look something like this:

    public void JoinCreate() {
      RoomOptions options = new RoomOptions();
      options.MaxPlayers = 10; //set however many players you would want here
    
      PhotonNetwork.JoinOrCreateRoom("oda1", options, TypedLobby.Default);
    }
    

    I highly recommend reading the error messages, seen at the bottom of the box that appeared when hovering over the code in question. I also recommend taking the time to read the documentation, and if you have, include what you have tried / searched before making a post here.