Search Unity

Third Party [PUN2] Player controller prefab not spawning

Discussion in 'Multiplayer' started by Wuna, Feb 18, 2022.

  1. Wuna

    Wuna

    Joined:
    Nov 4, 2015
    Posts:
    7
    So I create a room first for player to gather and then we go to the same scene if all players are ready. I'm expecting the player prefab will spawn and we can see each other but unfortunatelly no.
    So I'm guessing we are already joined a room before and we can't join to the same scene.
    Any help to solve this problem? Thanks
     
  2. toddkc

    toddkc

    Joined:
    Nov 20, 2016
    Posts:
    207
    Gonna need more info...
    You say you are expecting the player prefab to spawn, are you expecting that because you coded that?

    Ask better questions and get better answers.
     
  3. Wuna

    Wuna

    Joined:
    Nov 4, 2015
    Posts:
    7
    Ok then I'l' try to make it clear.
    So first I set the network settings like this
    Code (CSharp):
    1.  
    2. PhotonNetwork.SendRate = _sendRate;
    3. PhotonNetwork.SerializationRate = _serializationRate; //10
    4. PhotonNetwork.AutomaticallySyncScene = _automaticallySyncScene;
    5. PhotonNetwork.NickName = _nickName;
    6. PhotonNetwork.GameVersion = _gameVersion;
    7. PhotonNetwork.ConnectUsingSettings();
    And then I create room using this function
    Code (CSharp):
    1. if (!PhotonNetwork.IsConnected)
    2.                 return;
    3.  
    4.             RoomOptions options = new RoomOptions();
    5.             options.BroadcastPropsChangeToAll = true;
    6.             options.PublishUserId = true;
    7.             options.MaxPlayers = 4;
    8.  
    9.             Sandbox_NetworkConfig.Instance.SetCustomNumber();
    10.  
    11.             string roomName = _st[Random.Range(0, _st.Length)] + _st[Random.Range(0, _st.Length)] + _st[Random.Range(0, _st.Length)] + _st[Random.Range(0, _st.Length)] + _st[Random.Range(0, _st.Length)] + "";
    12.             PhotonNetwork.JoinOrCreateRoom(roomName, options, null);
    And after the players are gathered in a room the master client can start the game using this function
    Code (CSharp):
    1. PhotonNetwork.CurrentRoom.IsOpen = false;
    2.                 PhotonNetwork.CurrentRoom.IsVisible = false;
    3.                 PhotonNetwork.LoadLevel(1);
    Then after the level is loaded I bassically just spawn the prefab using this function
    Code (CSharp):
    1. public override void OnJoinedRoom()
    2.     {
    3.         base.OnJoinedRoom();
    4.         print("spawn");
    5.         spawnedPlayerPrefab = PhotonNetwork.Instantiate("NetworkAvatar", transform.position, transform.rotation);
    6.     }
    But for some reason the callback doesn't get called because I think the player already joined the room before so I can't get to make it work. Any solution for this?
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,072
    Read the docs.

    OnJoinedRoom: Called when the LoadBalancingClient entered a room, no matter if this client created it or simply joined.

    This is not called when you load a scene. You even took the room out of the matchmaking earlier.

    You're loading a scene. Any script in this could implement Start() to instantiate something.
     
  5. Wuna

    Wuna

    Joined:
    Nov 4, 2015
    Posts:
    7
    Ah I see. So I just need to instantiate it inside Start() ?
     
  6. rajababricz

    rajababricz

    Joined:
    Aug 23, 2022
    Posts:
    2
    please tell me i have a car multiplayer game in pun 2 my car prefab when spawns it not moves and spawns correctly