Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Photon Unity Networking (PUN) trouble with keeping track of players on each team

Discussion in 'Scripting' started by aLovedHater, Nov 17, 2015.

  1. aLovedHater

    aLovedHater

    Joined:
    Oct 12, 2014
    Posts:
    16
    What i want to happen is when the player connects to the room they pick team Red or Blue (1 or 2)
    The max players for each team is 5, i keep track of the players by using variables such as
    Code (CSharp):
    1. int currentRedPlayers = 0;
    2. int maxRedPlayers = 5;
    3. int currentBluePlayers = 0;
    4. int MaxBluePlayers = 5;

    I've been having a lot of trouble synchronizing this over the network..
    currently what i'm doing is when you join the Room you spawn a GameManagerObject for yourself using PhotonNetwork.Instantiate
    assuming you are the first to join the server, the variables currentRedPlayers and currentBluePlayers will be set to 0. then when you pick a team (lets say you pick red team)
    currentRedPlayers will jump to 1, so the 4 variables will look like this.
    Code (CSharp):
    1. currentRedPlayers = 1;
    2. maxRedPlayers = 5;
    3. currentBluePlayers = 0;
    4. MaxBluePlayers = 5;
    this works.

    Now say a second player joins and they choose the blue team, their variables will be
    Code (CSharp):
    1. currentRedPlayers = 0;
    2. maxRedPlayers = 5;
    3. currentBluePlayers = 1;
    4. maxBluePlayers = 5;

    My way of trying to get the two clients to update each others GameManagerObject's was to add a photon view and have it observe the object's script "GameManager"
    this is the script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameManager : MonoBehaviour
    5. {
    6.     public int currentRedPlayers = 0;
    7.     public int maxRedPlayers = 5;
    8.     public int currentBluePlayers = 0;
    9.     public int maxBluePlayers = 5;
    10.  
    11.     void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    12.     {
    13.         if (stream.isWriting)
    14.         {
    15.             //player sends info to network data
    16.             stream.SendNext(currentRedPlayers);
    17.             stream.SendNext(currentBluePlayers);
    18.         }
    19.         else
    20.         {
    21.             //player recieves info from network data (isReading)
    22.             currentRedPlayers = (int)stream.ReceiveNext();
    23.             currentBluePlayers = (int)stream.ReceiveNext();
    24.         }
    25.     }
    26. }
    27.  
    This is supposed to update each client's GameManagerObject so that each one will read variables as
    currentredPlayers = 1;
    maxRedplayers = 5;
    currentBluePlayers = 1;
    maxBluePlayers = 5;

    but instead it will only show their own, if i run client 1 in the unity editor and client 2 as a Build and Run, i can see in client one in the hierarchy that it shows both instantiates GameManagerObjects for each player, and it shows their right values as
    Code (CSharp):
    1. int currentRedPlayers = 1;
    2. int maxRedPlayers = 5;
    3. int currentBluePlayers = 0;
    4. int MaxBluePlayers = 5;
    and
    Code (CSharp):
    1. currentRedPlayers = 0;
    2. maxRedPlayers = 5;
    3. currentBluePlayers = 1;
    4. maxBluePlayers = 5;

    But they DONT update each other so that they both read as
    Code (CSharp):
    1. int currentRedPlayers = 1;
    2. int maxRedPlayers = 5;
    3. int currentBluePlayers = 1;
    4. int MaxBluePlayers = 5;
    I'm sorry if this is a newbie question but i am very much so a newbie when it comes to PUN, I've spent countless hours trying to find different ways to fix this, but this is about as close as I've come and I've hit a wall. Can anyone clue me in on how i can have each client's object update each other so they can read the current players in the room the correct way?
     
    Last edited: Nov 17, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. aLovedHater

    aLovedHater

    Joined:
    Oct 12, 2014
    Posts:
    16
    sorry everything looks different than it used to ><
     
  4. aLovedHater

    aLovedHater

    Joined:
    Oct 12, 2014
    Posts:
    16
    problem fixed, i should've been using RPC calls.