UNET - new client not seeing the updated state of a GameObject

Hi all,

I was testing with Unity’s UNET how to synchonize a simple TextMesh on a GameObject. When a player presses the Spacebar it sets it’s own InstanceID to the TextMesh on the GameObject and every Client sees the update! So that is working pretty well.

The problem that I have at this moment is that if a new Client joins, he won’t see the latest update of the TextMesh. I tried using [SyncVar(hook=“”)] or the OnStartClient to update when the new Client joins, but nothing works.

I used the Unity documention and many examples, but I just cant find a solution. In theorie it should not be that hard! :slight_smile: Can anyone explain how this can be done? I’m using the following code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class AddBlock : NetworkBehaviour {

    [SyncVar]
    [SerializeField] GameObject _t;

    void Start () {
        _t = GameObject.FindGameObjectWithTag("PlayerID");
    }
  
    void Update () {
        if (isLocalPlayer)
        {
            if (Input.GetKeyDown("space"))
            {
                _t.GetComponent<TextMesh>().text = gameObject.GetInstanceID().ToString();
                Cmd_ChangeText();
                Rpc_ChangeText();
            }
        }
    }

    [ClientRpc]
    void Rpc_ChangeText()
    {
        _t.GetComponent<TextMesh>().text = gameObject.GetInstanceID().ToString();
    }

    [Command]
    void Cmd_ChangeText()
    {
        _t.GetComponent<TextMesh>().text = gameObject.GetInstanceID().ToString();
    }
}

Thanks in advance!

Greetings,

Zarakashi

SyncVars are for simple data types such as Int, float, string etc.
You can’t sync gameobjects unless you yourself write the serialization code.

Thanks for the reply. To bad Unity doesn’t support that.
Although the SyncVar won’t work with the GameObject, the syncing is working as aspected when one af the clients presses the spacebar.

The problem still remains, how can I sent the last state of the GameObject to the new Client when he joins the server? I added a piece of code to the Update function that works, but this is not very efficient. There must be a way to run the sync function when a new Client joins, or am I wrong?

if (isServer)
        {
            Rpc_ChangeText(_t.GetComponent<TextMesh>().text.ToString());
        }