Search Unity

UNet - Commands are sent to server by one client, but not on the other.

Discussion in 'Scripting' started by Theronguard, Aug 11, 2019.

  1. Theronguard

    Theronguard

    Joined:
    Jun 14, 2019
    Posts:
    2
    Hi. I'm fairly new to Unity/c# and I wanted to make a project that includes networking (maybe I tackled it too early on). I'm using UNET (I know it's getting deprecated).

    The problem:
    Everything works fine when I have only 1 client connected, but when I built my project and ran 3 instances (1 host, 2 clients) my program began to break. I noticed that:
    -I host a server -> Everything is fine.
    -I connect to the server with the first client -> Everything is fine
    -I connect to the server with the second client -> The second client works fine, but the first stops working properly.
    -Whichever client connects last works properly.

    Code (csharp):
    1.  
    2.     //Client code in which I noticed strange behaviour
    3.     {  
    4.       woohoo = this.gameObject.transform.GetChild(0).GetChild(0);
    5.  
    6.     if (woohoo.GetComponent<MainCounter>().countOn)
    7.     {
    8.         audsrc.Play(); //This plays on the broken client
    9.         CmdUpdTxt(playerName.text); //But this doesn't seem to execute - why?
    10.     }
    11.  
    12. }
    13.        
    14.  
    15.     [Command]
    16.     void CmdUpdTxt(string plyname)
    17.     {
    18.         woohoo = this.gameObject.transform.GetChild(0).GetChild(0);
    19.         woohoo.GetComponent<MainCounter>().Countdown();
    20.         RpcUpdTxt(plyname);
    21.     }
    22.  
    Why does that happen :? Any help appreciated .
     
  2. Theronguard

    Theronguard

    Joined:
    Jun 14, 2019
    Posts:
    2
    Noticed another thing. If I add "isLocalPlayer" in here:

    Code (csharp):
    1.  
    2. if(isLocalPlayer) // <---------------
    3.        {  
    4.             woohoo = this.gameObject.transform.GetChild(0).GetChild(0);
    5.  
    6.             if (woohoo.GetComponent<MainCounter>().countOn)
    7.             {
    8.                 audsrc.Play();
    9.                 CmdUpdTxt(playerName.text);
    10.             }
    11.        }
    12.  
    The code doesn't execute -> the sound doesn't play on the broken client but works on the other.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Can you just post this script in its entirety?