Search Unity

Change players number every time a player enters

Discussion in 'Multiplayer' started by LiOn0X0HeaRt, Jul 1, 2015.

  1. LiOn0X0HeaRt

    LiOn0X0HeaRt

    Joined:
    Jul 14, 2014
    Posts:
    38
    Hi
    i have been trying to add a number to the server every time a player enters
    but i have been through hell i feel like the networking system doesn't belong to unity its not friendly at all
    i have spent almost a month and i still get different results than what i always expect
    anyways here is the code
    this code runs on start in game and if player is local
    Code (CSharp):
    1.  
    2. if(randomRangeTeam ==0)
    3. {
    4. //orange team
    5. CmdTeamNumbers(1,0);
    6.  
    7.  
    8. }
    9. if(randomRangeTeam == 1){
    10.  
    11.                    //blue team
    12.                 CmdTeamNumbers(0,1);
    13.        
    14.        
    15. }
    and here i receive
    Code (CSharp):
    1.  
    2.     [Command]
    3.     void CmdTeamNumbers(int orange , int blue)
    4.     {
    5.         orangeTeamNumber += orange;
    6.         blueTeamNumber += blue;
    7.  
    8.  
    9.     }
    orange and blueTeamNumber are synced by [SyncVar]
    i even tried to not sync them to make them only change on the server but it didn't work

    i understand that Cmd is called by client and invoked on server
    but the problem is the server doesn't get the message the values do not change
     
    Last edited: Jul 1, 2015
  2. LiOn0X0HeaRt

    LiOn0X0HeaRt

    Joined:
    Jul 14, 2014
    Posts:
    38
    Do i need to use isLocalPlayer == false?
    but how?
     
  3. Necromantic

    Necromantic

    Joined:
    Feb 11, 2013
    Posts:
    116
    What is the rest of the code? How do you set randomRangeTeam? Does the Script inherit from NetworkBehaviour (assuming it is since it uses isLocalPlayer)?
     
  4. LiOn0X0HeaRt

    LiOn0X0HeaRt

    Joined:
    Jul 14, 2014
    Posts:
    38
    its just random.range(0,2);
    in local same script
    ill post the code soon cuz i changed it a lot
     
  5. LiOn0X0HeaRt

    LiOn0X0HeaRt

    Joined:
    Jul 14, 2014
    Posts:
    38
    Sorry i changed the code a lot can't get it back
    but i solved the issue but still i have a question
    this is how i solved it
    Code (CSharp):
    1.     [Command]
    2.     void CmdTeamNumbersTotal(int orange, int blue,GameObject[] players)
    3.     {
    4.    
    5.        
    6.        
    7.         //RpcTeamNumbersTotal( orange, blue,players);
    8.         foreach(GameObject player in players)
    9.         {
    10.             //player.SendMessage("Players",orange,blue);
    11.             player.GetComponent<Sync2>().Players(orange,blue);
    12.            
    13.         }
    14.     }
    in the host pc i looked for all the blue and orange players and now the host knows exactly the number of all players
    and then to send them i stored all the players in an array and sent them the number
    through Cmd

    problem is i don't feel this is the proper way
    problem two SenMessage gave me an error
    ("the best overloaded method match for unityengine.gameobject.SendMessage(string,object,unityengine.sendmessage
    option") has some invalid arguments
     
  6. Necromantic

    Necromantic

    Joined:
    Feb 11, 2013
    Posts:
    116
    SendMessage only takes one object paramenter, you could encapsulate the data in an array like:
    Code (CSharp):
    1. player.SendMessage("Players", new int[]{ orange, blue });
    What's wrong with the RPC call? You should probably just have one static Class that keeps track of all that global network data and syncs it that all the players can access.
     
  7. LiOn0X0HeaRt

    LiOn0X0HeaRt

    Joined:
    Jul 14, 2014
    Posts:
    38
    I don't know what you mean by static and RPC call
    does the static matter when we use the networking system?
    i mean there is no pc that can reach any pc unless we send the information to it
    but when we use static we don't send the information we take it because there is only one variable with
    statics but how can we take it if its not in our pc? meaning there is not only one variable even if its static

    i might be wrong i don't know how statics work with the network system but i hope you may correct me

    and for the RPC call i didn't use any RPC's
    are you suggesting that i use it with static?

    sorry i don't have networking background but hope you can help me little bit more