Search Unity

Third Party PUN Client is not the MasterClient in the room (Instantiate)

Discussion in 'Multiplayer' started by juniorforestS, Jul 16, 2017.

  1. juniorforestS

    juniorforestS

    Joined:
    Apr 18, 2015
    Posts:
    47
    Failed to InstantiateSceneObject prefab. AirPlane, Client is not the MasterClient in the room

    image and script below:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FlareAirDrop : MonoBehaviour {
    6.  
    7.     public GameObject AirPlane;
    8.     public GameObject AirDrop;
    9.     public float speed;
    10.     private bool suport = false;
    11.     public PhotonView photonView;
    12.     private bool suport2 = false;
    13.  
    14.     [PunRPC]
    15.     public void Airplanespawn (){
    16.  
    17.         AirPlane = PhotonNetwork.InstantiateSceneObject ("AirPlane", new Vector3 (Random.Range (-1000, 1000), 230, -100), Quaternion.identity, 0, null) as GameObject;
    18.  
    19.         Vector3 relativePos = AirPlane.transform.position - transform.position;
    20.         Quaternion rotation = Quaternion.LookRotation(relativePos);
    21.         AirPlane.transform.rotation = rotation;
    22.     }
    23.  
    24.     [PunRPC]
    25.     public void airdropspawn (){
    26.  
    27.         AirDrop = PhotonNetwork.InstantiateSceneObject ("AirDrop", new Vector3 (AirPlane.transform.position.x, AirPlane.transform.position.y - 10, AirPlane.transform.position.z), Quaternion.identity, 0, null) as GameObject;
    28.  
    29.     }
    30.  
    31.     void Update () {
    32.  
    33.         if (Input.GetKeyDown ("e")) {
    34.  
    35.             if (photonView.isMine) {
    36.                 gameObject.GetComponent<PhotonView> ().RPC ("Airplanespawn", PhotonTargets.All, null);
    37.                 suport2 = true;
    38.             }
    39.         }
    40.  
    41.         float step = speed * Time.deltaTime;
    42.  
    43.         if (suport == false && suport2 == true) {
    44.  
    45.             AirPlane.transform.position = Vector3.MoveTowards (AirPlane.transform.position, new Vector3 (transform.position.x, 230, transform.position.z), step);
    46.        
    47.         }
    48.  
    49.         if (suport == true) {
    50.            
    51.             AirPlane.transform.Translate (0, 0.2f, -step);
    52.  
    53.         }
    54.  
    55.         if (AirPlane.transform.position.z >= transform.position.z && suport == false) {
    56.  
    57.                 suport = true;
    58.                 suport2 = true;
    59.                 Debug.LogAssertionFormat ("AirDroped");
    60.                 photonView.RPC ("airdropspawn", PhotonTargets.All, null);
    61.  
    62.             }
    63.         }
    64.     }

    https://uploaddeimagens.com.br/images/000/997/078/full/sdawdw.png?1500237579
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    Well, the error says it all: Only the Master Client of a room can instantiate scene objects.
    You can place networked objects in the scene and the Master Client can use InstantiateSceneObject at runtime.
    In all other cases, use PhotonNetwork.Instantiate for networked objects.
     
  3. juniorforestS

    juniorforestS

    Joined:
    Apr 18, 2015
    Posts:
    47
    I tried to put only Instantiate, but it only works for the player who owns the room (masteclient) and not the other player that comes in after, how do I do for everyone? This has been my problem, a non-masterclient player is not able to instantiate "AirPlane" for all players....
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
  5. juniorforestS

    juniorforestS

    Joined:
    Apr 18, 2015
    Posts:
    47
    So how should I solve this? My connection script below, is there something wrong?
    Code (CSharp):
    1.  
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class mt_Connection : Photon.MonoBehaviour
    6. {
    7.     string PlayerName = "Player";
    8.     string DefaultPlayerName = "Player";
    9.  
    10.     static mt_Lobby m_Lobby = null;
    11.     static mt_Lobby Lobby
    12.     {
    13.         get
    14.         {
    15.             if (m_Lobby == null)
    16.                 m_Lobby = FindObjectOfType<mt_Lobby>();
    17.             return m_Lobby;
    18.         }
    19.     }
    20.  
    21.     List<PhotonPlayer> m_PlayerList = new List<PhotonPlayer>();
    22.     public List<PhotonPlayer> PlayerList
    23.     {
    24.         get
    25.         {
    26.             m_PlayerList.Clear();
    27.             m_PlayerList.TrimExcess();
    28.             foreach (PhotonPlayer players in PhotonNetwork.playerList)
    29.             {
    30.                 m_PlayerList.Add(players);
    31.             }
    32.             return m_PlayerList;
    33.         }
    34.     }
    35.  
    36.     void Awake(){
    37.  
    38.         PhotonNetwork.automaticallySyncScene = true;
    39.  
    40.     }
    41.  
    42.     public void JoinServer(bool create, bool random)
    43.     {
    44.         if (create && !random) CreateRoom();
    45.         else if (!create && !random) JoinRoom();
    46.         else if (!create && random) JoinRandom();
    47.  
    48.     }
    49.  
    50.     void CreateRoom()
    51.     {
    52.      
    53.         RoomInfo[] rooms = PhotonNetwork.GetRoomList();
    54.         string roomName = Lobby.RoomName;
    55.         for (int i = 0; i < rooms.Length; i++)
    56.         {
    57.             if (roomName.Equals(rooms[i].Name))
    58.             {
    59.                 roomName += "(" + PhotonNetwork.GetRoomList().Length + 1 + ")";
    60.             }
    61.         }
    62.         Debug.Log("trying to create room: " + roomName);
    63.  
    64.         ExitGames.Client.Photon.Hashtable customPropertiesToSet = new ExitGames.Client.Photon.Hashtable();
    65.         customPropertiesToSet.Add("MapName", Lobby.MapName);
    66.         customPropertiesToSet.Add("GameMode", Lobby.GameMode);
    67.         customPropertiesToSet.Add("MapImage", Lobby.SelectedMap);
    68.         customPropertiesToSet.Add("GameLength", Lobby.GameLength);
    69.         customPropertiesToSet.Add("Password", Lobby.Password);
    70.         //customPropertiesToSet.Add("Team", Lobby.Team);
    71.  
    72.         string[] customProperties = new string[5];
    73.         customProperties[0] = "MapName";
    74.         customProperties[1] = "GameMode";
    75.         customProperties[2] = "MapImage";
    76.         customProperties[3] = "GameLength";
    77.         customProperties[4] = "Password";
    78.         PhotonNetwork.CreateRoom(roomName, new RoomOptions() { MaxPlayers = (byte)Lobby.MaxPlayers, IsVisible = true, IsOpen = true, CustomRoomProperties = customPropertiesToSet, CleanupCacheOnLeave = true, CustomRoomPropertiesForLobby = customProperties }, null);
    79.  
    80.     }
    81.  
    82.     bool JoinRoom()
    83.     {
    84.         Debug.Log("trying to join room: " + Lobby.RoomName);
    85.         return PhotonNetwork.JoinRoom(Lobby.RoomName);
    86.  
    87.  
    88.     }
    89.  
    90.     void JoinRandom()
    91.     {
    92.         Debug.Log("trying to join a random room");
    93.         PhotonNetwork.JoinRandomRoom();
    94.  
    95.     }
    96.  
    97.     public void Reconnect()
    98.     {
    99.         if (PhotonNetwork.connectionStateDetailed != ClientState.Disconnected && PhotonNetwork.connectionStateDetailed != ClientState.PeerCreated)
    100.         {
    101.             PhotonNetwork.Disconnect();
    102.         }
    103.  
    104.         PhotonNetwork.ConnectUsingSettings("0.1");
    105.     }
    106.  
    107.     void OnJoinedLobby()
    108.     {
    109.         Lobby.currentWindow = mt_Lobby.CURRENTWINDOW.MAIN;
    110.         Lobby.InvokeRepeating("ServerList", 1, Lobby.ServerListRefresh);
    111.  
    112.         if (PlayerName == DefaultPlayerName && PlayerPrefs.GetString("PlayerName") != "") PlayerName = PlayerPrefs.GetString("PlayerName");
    113.         PhotonNetwork.playerName = PlayerName;
    114.         Debug.Log("PlayerName set to " + PhotonNetwork.playerName);
    115.     }
    116.  
    117.     void OnLeftLobby()
    118.     {
    119.  
    120.     }
    121.     void OnReceivedRoomListUpdate()
    122.     {
    123.         Debug.Log("rooms updated: " + PhotonNetwork.GetRoomList().Length);
    124.         Lobby.MessageText(null, Color.red, 0);
    125.     }
    126.  
    127.     void OnJoinedRoom()
    128.     {
    129.         Lobby.currentWindow = mt_Lobby.CURRENTWINDOW.NONE;
    130.         Debug.Log("Joined Room");
    131.  
    132.         List<PhotonPlayer> playerList = new List<PhotonPlayer>();
    133.         playerList.Clear();
    134.         playerList = PlayerList;
    135.         for (int p = 0; p < playerList.Count; p++)
    136.         {
    137.             Debug.Log("Players connected: " + playerList[p].NickName);
    138.             if (playerList[p] != PhotonNetwork.player)
    139.             {
    140.                 if (playerList[p].NickName == PhotonNetwork.playerName)
    141.                 {
    142.                     PhotonNetwork.playerName = PhotonNetwork.playerName + "(" + 1 + ")";
    143.                 }
    144.             }
    145.         }
    146.  
    147.         PhotonNetwork.LeaveLobby();
    148.         PhotonNetwork.LoadLevel (Lobby.RoomName);
    149.  
    150.     }
    151.  
    152.     void OnFailedToConnectToPhoton(DisconnectCause er)
    153.     {
    154.         Debug.LogWarning("mt_Connection: Failed To Connect To Cloud: " + er);
    155.         Lobby.MessageText("Waiting for Internet Connection...", Color.red, 999, TextAnchor.UpperLeft);
    156.         InvokeRepeating("Reconnect", 3.5f, 3.5f);
    157.     }
    158.  
    159.     void OnConnectionFail(DisconnectCause er)
    160.     {
    161.         Debug.LogWarning("mt_Connection: Connection Failed: " + er);
    162.         Lobby.MessageText("Waiting for Internet Connection...", Color.red, 999, TextAnchor.UpperLeft);
    163.         InvokeRepeating("Reconnect", 3.5f, 3.5f);
    164.     }
    165.  
    166.     void OnConnectedToMaster()
    167.     {
    168.         Debug.Log("Connected to Master");
    169.         Lobby.MessageText("Connected to Master", Color.red, 3, TextAnchor.UpperLeft);
    170.         CancelInvoke("Reconnect");
    171.     }
    172.  
    173.     void OnPhotonRandomJoinFailed()
    174.     {
    175.         Debug.Log("mt_Connection: No random room available, so we create one.");
    176.         CreateRoom();
    177.     }
    178. }
    179.  
     
    Last edited: Jul 18, 2017
  6. juniorforestS

    juniorforestS

    Joined:
    Apr 18, 2015
    Posts:
    47
    Last edited: Jul 19, 2017
  7. juniorforestS

    juniorforestS

    Joined:
    Apr 18, 2015
    Posts:
    47
  8. juniorforestS

    juniorforestS

    Joined:
    Apr 18, 2015
    Posts:
    47
    I tried this:

    In my airplane prefab on Resources folder

    Code (CSharp):
    1. void OnPhotonInstantiate(PhotonMessageInfo info)
    2.     {
    3.         FlareAirDrop myObject = GameObject.FindObjectOfType<FlareAirDrop>();
    4.         myObject.AirPlane = gameObject;
    5.     }
    but nothing again =/
     
    Last edited: Jul 21, 2017