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.

Unity Multiplayer [Solved] String not being sent to server?

Discussion in 'Multiplayer' started by Cynikal, Jul 28, 2018.

  1. Cynikal

    Cynikal

    Joined:
    Oct 29, 2012
    Posts:
    122
    Ok. I have 3 main scripts I deal with.

    GameMain (on the player object, local authority)
    GameManager (on it's own gameobject, no authority)
    GameMain_Server (on it's own object, server authority)

    I am using Master Server Kit.

    On Game Manager, I have a function:

    Code (CSharp):
    1.     [ClientRpc]
    2.     public void RpcSetupAnswer()
    3.     {
    4.         y = 230f;
    5.  
    6.         myLocalPlayer.AnswerPoints = 0;
    7.         myLocalPlayer.SelectedAnswersOwnerUsername = "Nobody";
    8.        
    9.         ChangeStatusOfSubmitAnswer(false);
    10.         AnswerContainer.SetActive(true);
    11.        
    12.         foreach (GameMain a in GameMain.players)
    13.         {
    14.             if (!a.GetComponent<NetworkIdentity>().isLocalPlayer)
    15.             {
    16.                 GameObject answer = Instantiate(answerprefab);
    17.                 answer.GetComponent<SetAnswer_GO>().SetButtonInfo(a.myAnswer, a.Username, () => { myLocalPlayer.AnswerButtonClicked(a.Username); });
    18.  
    19.                 answer.transform.SetParent(GameObject.Find("AnswersContainer").transform, false);
    20.                 answer.transform.localPosition = new Vector3(-30.5f, y, 0f);
    21.                 answerprefabs.Add(answer);
    22.                 y -= 55f;
    23.             }
    24.         }
    25.     }
    The issue is with the "
                    answer.GetComponent<SetAnswer_GO>().SetButtonInfo(a.myAnswer, a.Username, () => { myLocalPlayer.AnswerButtonClicked(a.Username); });
    " portion.

    On the CLIENT, the username gets sent. (verified with debug.log), answerbuttonclicked looks like:

    Code (CSharp):
    1.     [SyncVar (hook = "OnSelectedAnswerOwnerUsernameChanged")] public string SelectedAnswersOwnerUsername;
    2.     public void AnswerButtonClicked(string submitter)
    3.     {
    4.        
    5.         Debug.Log("We've selected: "+ submitter +"'s answer!");
    6.         this.SelectedAnswersOwnerUsername = submitter;
    7.        
    8.         CmdAnswerPoints();
    9.     }
    10.  
    11.     void OnSelectedAnswerOwnerUsernameChanged(string value)
    12.     {
    13.         Debug.Log("Selected Answer Owner Name changed to: "+ value);
    14.         this.SelectedAnswersOwnerUsername = value;
    15.     }
    On the client, I do get successful results of: "We've selected: Username's answer!".

    Now, it calls the command:
    Code (CSharp):
    1.     [Command]
    2.     public void CmdAnswerPoints()
    3.     {
    4.         Debug.Log("Adding +1 to: "+ SelectedAnswersOwnerUsername);
    5.         gm.AddPointToPlayer(this.SelectedAnswersOwnerUsername);
    6.     }
    On the SERVER, SelectedAnswersOwnerUsername is blank.

    For the life of me, I can NOT figure out why?

    Is it because i'm calling the initial function from a ClientRpc? Or is there just something i'm not seeing?
     
  2. Cynikal

    Cynikal

    Joined:
    Oct 29, 2012
    Posts:
    122
    Solution: Instead of pulling from a local variable, i just passed the variable along with the cmd.