Search Unity

SyncVar updating prefab value, not player value.

Discussion in 'Multiplayer' started by Murfbard, May 29, 2020.

  1. Murfbard

    Murfbard

    Joined:
    Jan 11, 2014
    Posts:
    7
    I have a UI Button that calls SendNameToServer() in a script that is attached to the Player GameObject. This function finds a textbox in the game and adds the text in that to MyName, a public string.

    Code (CSharp):
    1. public class PlayerMove : NetworkBehaviour
    2. {
    3.     [SyncVar]
    4.     public string MyName;
    5.  
    6.     private string txtName;
    7.  
    8.     void Update()
    9.     {  
    10.         gameObject.GetComponentInChildren<TextMesh>().text = MyName;
    11.     }
    12.  
    13.     // Called on button click
    14.     public void SendNameToServer()
    15.     {
    16.         // Gets val from textbox      
    17.         txtName = GameObject.Find("VarText").GetComponent<InputField>().text;
    18.         MyName = txtName;
    19.     }
    20. }
    This does not update the instantiated player Gameobject, but only the prefab in my Assets library!


    This is the instantiated player:


    This is the prefab in the Assets folder, with the updated value:



    What am I doing wrong?
     
  2. dahiyabunty1

    dahiyabunty1

    Joined:
    Jan 22, 2020
    Posts:
    68
    try..
    gameobject g = instanitiate(GameObject.Find("VarText"));
     
  3. Murfbard

    Murfbard

    Joined:
    Jan 11, 2014
    Posts:
    7
    I tried that, as I thought that just creates an additional VarText object.
     
  4. dahiyabunty1

    dahiyabunty1

    Joined:
    Jan 22, 2020
    Posts:
    68
    ok.. may be try drag and drop public InputField instead of GameObject.Find();
     
  5. Murfbard

    Murfbard

    Joined:
    Jan 11, 2014
    Posts:
    7
    That does not work.
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You're almost certainly calling SendNameToServer() from the UI button on the player Prefab instead of the instance of it in the scene. Pretty much any time you're unexpectedly seeing changes to the prefab instead of the one in the scene is because you're accidentally using the prefab's reference instead of a reference to the spawned player.

    If you have a reference to the player prefab in the assets folder anywhere in your scene other than in the NetworkManager, you're probably doing it wrong. The NetworkManager instantiates/spawns the player prefab, and it is unlikely any other script in your game needs to also do that, so there is almost no point to any other script having a reference to the original prefab.

    As a separate issue, your SendNameToServer() method doesn't send anything to the server. It just forces the MyName SyncVar to be out of sync with the server and other clients. Whenever your code forces a SyncVar to be out of sync with the server's version, it is almost always a bug in your code which is doing so. You should only ever set the value of a SyncVar on the server, or on the client within a SyncVar Hook method.
     
    Last edited: May 29, 2020