Search Unity

Client can't change variable

Discussion in 'Multiplayer' started by mika132132, Apr 19, 2020.

  1. mika132132

    mika132132

    Joined:
    Aug 6, 2017
    Posts:
    3
    Hello. I have problem where my multiplayer game Client can't change variable value. Every time i start a game and host click this button money value changes everybody. But if i click button with client. Nothing happen.

    First there is a button. It's go like this:

    Code (CSharp):
    1.  
    2.     if(GUI.Button(new Rect(520, 60, 200, 30), "More money 1000 (testUse)"))
    3.     {
    4.     globalDatas.CmdUpdateMoney(globalDatas._CompanyList[companyProfile], 1000, companyProfile);
    5.      }
    6.  
    globalDatas is variable where is saved GlobalData.cs file. That function what you see in there (ChangeMoney) go's like this:

    Code (CSharp):
    1.  
    2.         [Command]
    3.         public void CmdUpdateMoney(Companys comp, float valueChange, int myIndex)
    4.         {
    5.             comp.Money += valueChange;
    6.             _CompanyList[myIndex] = comp;
    7.         }
    8.  
    Now when Host click that button money value changes, but when client click nothing happen.

    And some extra info:
    Here is how that _CompanyList variable is made:

    Code (CSharp):
    1.  
    2.         public class allCompanys : SyncListStruct<Companys> { };
    3.         public allCompanys _CompanyList = new allCompanys();
    4.  

    And if that have some help. Here is how Struct is made. There is that Stock Buy system and that dosen't work too. It's same. Only Server can buy not client:



    Code (CSharp):
    1.  
    2.     public struct Companys
    3.     {
    4.         public string companyName;
    5.         public float Money;
    6.         public float stocksValue;
    7.         public float stockPrice;
    8.         public Character Founder;
    9.  
    10.         public stocksOwners[] allOwners;
    11.  
    12.         public Companys(string name, float mon, float stockvalue, float stockprice, Character founder)
    13.         {
    14.             this.companyName = name;
    15.             this.Money = mon;
    16.             this.stocksValue = stockvalue;
    17.             this.stockPrice = stockprice;
    18.             this.Founder = founder;
    19.             this.allOwners = new stocksOwners[100];
    20.         }
    21.         [Command]
    22.         public void CmdBuyStocks(Character whoAmI, float howMuch)
    23.         {
    24.             if(this.allOwners[0].StockOwner == whoAmI)
    25.             {
    26.  
    27.             }
    28.             bool hereIam = false;
    29.             for (int i = 0; i < this.allOwners.Length; i++)
    30.             {
    31.                 if (this.allOwners[i] != null)
    32.                 {
    33.                     if (this.allOwners[i].StockOwner == whoAmI)
    34.                     {
    35.                         this.allOwners[i].howMuch += howMuch;
    36.                         hereIam = true;
    37.                         break;
    38.                     }
    39.                 }
    40.             }
    41.             if(!hereIam)
    42.             {
    43.                 this.CmdCreateStockOwner(whoAmI, howMuch);
    44.             }
    45.         }
    46.  
    47.         [Command]
    48.         public void CmdCreateStockOwner(Character myChar, float howMuch)
    49.         {
    50.             for (int i = 0; i < this.allOwners.Length; i++)
    51.             {
    52.                 if(this.allOwners[i].howMuch <= 0)
    53.                 {
    54.                     this.allOwners[i] = new stocksOwners(myChar, howMuch);
    55.                     break;
    56.                 }
    57.             }
    58.             Debug.Log("Data has send to server");
    59.             return;
    60.         }
    61.  
     
  2. Deleted User

    Deleted User

    Guest

    Commands are only executed on server/host. You need a targetrpc to call the command on a client or a clientrpc to call it on all clients.
     
  3. mika132132

    mika132132

    Joined:
    Aug 6, 2017
    Posts:
    3
    Okay. Situation is this:
    I have code in my UI.cs script. Which is attached to PlayerObject. Then there is PlayerObject.cs which is attached too in PlayerObject. PlayerObject is spawned object when client join in game.

    I have button in UI.cs file which go in PlayerObject.cs file because Authority need that when i read google little bit.

    This is in UI.cs file place where button is:
    Code (CSharp):
    1.             if(GUI.Button(new Rect(520, 60, 200, 30), "Lisää rahaa 1000"))
    2.             {
    3.                 Debug.Log("Do something.");
    4.                 if (isServer)
    5.                 {
    6.                     globalDatas.RpcUpdateMoney(globalDatas._CompanyList[companyProfile], 1000, companyProfile);
    7.                 }
    8.                 else
    9.                 {
    10.                     Debug.Log("Go to MoreMoney.");
    11.                     gameObject.GetComponent<PlayerObject>().UpdateMoney(globalDatas._CompanyList[companyProfile], 1000, companyProfile);
    12.                 }
    13.             }
    I tested that function works fine by adding this F2 button in PlayerObject.cs file. If i press F2 money rise. But if i press that UI button nothing happen.

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (!isLocalPlayer)
    4.         {
    5.             return;
    6.         }
    7.         if(Input.GetKeyUp(KeyCode.F2))
    8.         {
    9.             GlobalDatas globalDatas;
    10.             GameObject datas;
    11.  
    12.             datas = GameObject.Find("GlobalDatas");
    13.             globalDatas = datas.GetComponent<GlobalDatas>();
    14.  
    15.             if (isServer)
    16.             {
    17.                 globalDatas.RpcUpdateMoney(globalDatas._CompanyList[0], 1000, 0);
    18.             }
    19.             else
    20.             {
    21.                 CmdMoreMoney(globalDatas._CompanyList[0], 1000, 0);
    22.             }
    23.         }
    24.     }
    In PlayerObject.cs file i have those both functions. It's these.
    Code (CSharp):
    1.  
    2.     public void UpdateMoney(Companys comp, float valueChange, int myIndex)
    3.     {
    4.         comp.RpcChangeMoney(valueChange);
    5.  
    6.         CmdMoreMoney(comp, valueChange, myIndex);
    7.     }
    8.     [Command]
    9.     public void CmdMoreMoney(Companys comp, float valueChange, int myIndex)
    10.     {
    11.         comp.RpcChangeMoney(valueChange);
    12.         GlobalDatas globalDatas;
    13.         GameObject datas;
    14.  
    15.         datas = GameObject.Find("GlobalDatas");
    16.         globalDatas = datas.GetComponent<GlobalDatas>();
    17.  
    18.         globalDatas.RpcUpdateMoney(comp, valueChange, myIndex);
    19.     }
    And problem is only client site. When host press button money rise like it should.

    And when Client press F2 money rise like it should. But why not when i press that UI button?
     
  4. Deleted User

    Deleted User

    Guest

    Have you assigned the OnClick event on your button object in the editor window. You have to choose the script and then a dropdown will appear where you can choose a specific function defined in the script which will be executed when the button is clicked.