Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

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:
    4
    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:
    4
    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:
    5,960
    Can you just post this script in its entirety?
     
  4. Theronguard

    Theronguard

    Joined:
    Jun 14, 2019
    Posts:
    4
    I'll post it tomorrow, after I rename all the embarrassingly named functions and variables.
     
  5. Theronguard

    Theronguard

    Joined:
    Jun 14, 2019
    Posts:
    4
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Networking;
    6. using TMPro;
    7.  
    8.  
    9.  
    10. public class PlayerMGR : NetworkBehaviour
    11. {
    12.     public AudioClip audclip;
    13.     public AudioSource audsrc;
    14.     private GameObject[] ctdwns;
    15.     private GameObject[] startImg;
    16.     public Transform woohoo;
    17.     public TMP_InputField playerName;
    18.  
    19.  
    20.     private void Start()
    21.     {
    22.         audsrc.clip = audclip;
    23.         startImg = GameObject.FindGameObjectsWithTag("urzk");
    24.         foreach (GameObject rsp in startImg)
    25.         {
    26.             Destroy(rsp, 0);  //When you're not connected "NOT CONNECTED" images are displayed. When you connect this destroys them.
    27.         }
    28.     }
    29.  
    30.     public void whoClicked(string txt)
    31.     {
    32.         CmdClickSend(txt);
    33.     }
    34.  
    35.     [Command]
    36.     void CmdClickSend(string nick)
    37.     {
    38.         woohoo = this.gameObject.transform.GetChild(0).GetChild(4);
    39.         woohoo.GetComponent<TextMeshProUGUI>().text = nick;
    40.         RpcClickSend(nick);
    41.     }
    42.  
    43.     [ClientRpc]
    44.     void RpcClickSend(string nick)
    45.     {
    46.         woohoo = this.gameObject.transform.GetChild(0).GetChild(4);
    47.         woohoo.GetComponent<TextMeshProUGUI>().text = nick;
    48.     }
    49.  
    50.     public void ToServerPls()
    51.     {
    52.  
    53.         if (isServer)
    54.         {
    55.  
    56.  
    57.             woohoo = this.gameObject.transform.GetChild(0).GetChild(0); //This is the child counter object
    58.             //RpcUpdTxt();
    59.             RpcUpdTxt(playerName.text);
    60.             if (woohoo.GetComponent<MainCounter>().countOn == true)
    61.             {
    62.                 audsrc.Play();
    63.                 whoClicked(playerName.text);
    64.             }
    65.             else
    66.             {
    67.                 whoClicked("");
    68.             }
    69.             woohoo = this.gameObject.transform.GetChild(0).GetChild(0);
    70.             woohoo.GetComponent<MainCounter>().Countdown();
    71.             ctdwns = GameObject.FindGameObjectsWithTag("counters");
    72.  
    73.  
    74.             foreach (GameObject rsp in ctdwns)
    75.             {
    76.                 if (rsp.transform != woohoo)
    77.                 {
    78.                     Destroy(rsp, 0);
    79.                 }
    80.             }
    81.  
    82.         }
    83.  
    84.        if(isLocalPlayer)
    85.         {
    86.             woohoo = this.gameObject.transform.GetChild(0).GetChild(0);
    87.  
    88.             if (woohoo.GetComponent<MainCounter>().countOn)
    89.             {
    90.                 audsrc.Play();
    91.                 CmdUpdTxt(playerName.text);
    92.      
    93.             }
    94.  
    95.         }
    96.  
    97.  
    98.     }
    99.  
    100.     [Command]
    101.     void CmdUpdTxt(string plyname)
    102.     {
    103.         woohoo = this.gameObject.transform.GetChild(0).GetChild(0);
    104.         woohoo.GetComponent<MainCounter>().Countdown();
    105.         RpcUpdTxt(plyname);
    106.     }
    107.  
    108.     public void SetNumber() //There's an ui input box which determines from what number the countdown will, well, count down. And this updates the number.
    109.     {
    110.         woohoo = this.gameObject.transform.GetChild(0).GetChild(0);
    111.  
    112.  
    113.         if (woohoo.GetComponent<MainCounter>().input.text != "" && isServer)
    114.         {
    115.             woohoo.GetComponent<MainCounter>().numberSet = int.Parse(woohoo.GetComponent<MainCounter>().input.text);
    116.             //Debug.Log(numberSet);
    117.         }
    118.         if (isClient && woohoo.GetComponent<MainCounter>().input.text != "")
    119.         {
    120.             RpcUpdNumSet(int.Parse(woohoo.GetComponent<MainCounter>().input.text));
    121.         }
    122.     }
    123.  
    124.  
    125.     [ClientRpc]
    126.     void RpcUpdNumSet(int num) //Sets the current counter number on the client
    127.     {
    128.         if (!isServer)
    129.         {
    130.             woohoo = this.gameObject.transform.GetChild(0).GetChild(0);
    131.             woohoo.GetComponent<MainCounter>().numberSet = num;
    132.         }
    133.     }
    134.  
    135.     [ClientRpc]
    136.     void RpcUpdTxt(string plyname)
    137.     {
    138.         if (!isServer)
    139.         {
    140.                 whoClicked(plyname);
    141.                 woohoo = this.gameObject.transform.GetChild(0).GetChild(0);
    142.                 woohoo.GetComponent<MainCounter>().Countdown();
    143.  
    144.                 ctdwns = GameObject.FindGameObjectsWithTag("psiury");
    145.                 foreach (GameObject rsp in ctdwns)
    146.                 {
    147.                     if (rsp.transform != woohoo)
    148.                     {
    149.                         Destroy(rsp, 0);
    150.                     }
    151.                 }
    152.         }
    153.     }
    154. }
    155.  
    156.  
    Sorry for the lack of comments - it's a prototype just to learn some networking. As I stated earlier, I'm a newbie at it. This script is a mish mash of me trying to figure out how to communicate between the server and the client.
    If you need anything explained, I'll do it.

    The code above is a player prefab (spawned by the network manager + it has network identity with local player authority checked). The player is also a parent of few UI elements. This program is a counter that counts down (countdown stuff is inside another script of course, but all of the networking is done on the player prefab as it didn't work if I put it in any child object). The counter stops when one of the players presses the stop button, which displays his nick to all other clients. The stop button also starts the countdown (only server can start it). When the button is preesed it runs
    Code (csharp):
    1. ToServerPls()
    Sorry for this mess of a code, and thanks for any help.
     
    Last edited: Aug 14, 2019 at 1:10 PM