Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Third Party [PHOTON]Sync Instantiate UI to all players

Discussion in 'Multiplayer' started by Moddwyn, Dec 6, 2017.

  1. Moddwyn

    Moddwyn

    Joined:
    Jan 27, 2016
    Posts:
    49
    So basically when I die, i want everyone with a photonview and a tag of player to instantiate a UI. I tried this with instantiating using RPC and It didnt work. I noticed that when everyone calls this RPC, it wont show to everyone's UI but only to mine because the rpc is instantiating the UI to MY canvas. How do I make it so that when I die, everyone will instantiate this UI?

    I also want to know how to get all the players in the room and get a script from each of those players that runs the instantiation of the UI.
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,737
    You can get the list of currently connected clients using PhotonNetwork.PlayerList, which returns an array of PhotonPlayer objects; You can then use this array in a loop to send an RPC to each player using the RPC version that takes a PhotonPlayer as the target.
     
    Last edited: Dec 6, 2017
  3. Moddwyn

    Moddwyn

    Joined:
    Jan 27, 2016
    Posts:
    49
    Actually you can't access a PhotonPlayer's components with PhotonNetwork.PlayerList, i can can only get ids, names, teams, stuff like that.
     
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,737
    In your case you wouldn't use the PhotonPlayer object to directly access the components on the player object; What you would do is have an RPC on one of the scripts on your player object which enables the UI, and then use the list of PhotonPlayers to invoke that RPC on each of the player objects.

    However if you wanted to find the player object that belongs to a PhotonPlayer, you could just find all of the player objects in your scene, and compare their photonView.ownerID with the PhotonPlayer.ID to find a match.
     
  5. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,079
    While one player calls the RPC, every client in the room will execute it. That's making it a remote procedure call (RPC).
    In Photon, RPCs are executed on a specific GameObject with a PhotonView.

    Let one player call the RPC and every client should be able to execute it.
     
  6. Moddwyn

    Moddwyn

    Joined:
    Jan 27, 2016
    Posts:
    49
    Code (CSharp):
    1. public void Die(){
    2.         GetComponent <PhotonView> ().RPC ("SendFeed", PhotonTargets.Others, killersName);
    3.         PhotonNetwork.Destroy (gameObject);
    4.     }
    Code (CSharp):
    1. [PunRPC]
    2.     public void SendFeed(string killer){
    3.         GameObject newFeed = GameObject.Instantiate (feedPrefab);
    4.         newFeed.transform.SetParent (GameObject.Find ("KillFeed").transform);  
    5.         newFeed.transform.GetChild (0).GetComponent <Text> ().text = (killer + " killed " + GetComponent <PhotonView> ().owner.NickName);
    6.         newFeed.transform.localScale = new Vector3 (1, 1, 1);
    7.     }
    So this is my code rn, I had the player who died call the RPC to PhotonTargets.Others, so everyone else will instantiate the newFeed (
    Code (CSharp):
    1. GameObject newFeed = GameObject.Instantiate (feedPrefab);
    ), except the one that died. But I tried this but no one instantiate the newFeed (it doesn't show). Why does this happen because everyone instantiate the newfeed except the person that died, why does this doesn't work? And how do I fix this?